I\'m trying to implement a query in LINQ that uses a left outer join with multiple conditions in the ON clause.
I\'ll use the example of the following two tables
You can't do it like that. The join
clause (and the Join()
extension method) supports only equijoins. That's also the reason, why it uses equals
and not ==
. And even if you could do something like that, it wouldn't work, because join
is an inner join, not outer join.
You just need to name the anonymous property the same on both sides
on new { t1.ProjectID, SecondProperty = true } equals
new { t2.ProjectID, SecondProperty = t2.Completed } into j1
Based on the comments of @svick, here is another implementation that might make more sense:
from t1 in Projects
from t2 in Tasks.Where(x => t1.ProjectID == x.ProjectID && x.Completed == true)
.DefaultIfEmpty()
select new { t1.ProjectName, t2.TaskName }
Here you go with:
from b in _dbContext.Burden
join bl in _dbContext.BurdenLookups on
new { Organization_Type = b.Organization_Type_ID, Cost_Type = b.Cost_Type_ID } equals
new { Organization_Type = bl.Organization_Type_ID, Cost_Type = bl.Cost_Type_ID }
This works fine for 2 tables. I have 3 tables and on clause has to link 2 conditions from 3 tables. My code:
from p in _dbContext.Products join pv in _dbContext.ProductVariants on p.ProduktId equals pv.ProduktId join jpr in leftJoinQuery on new { VariantId = pv.Vid, ProductId = p.ProduktId } equals new { VariantId = jpr.Prices.VariantID, ProductId = jpr.Prices.ProduktID } into lj
But its showing error at this point: join pv in _dbContext.ProductVariants on p.ProduktId equals pv.ProduktId
Error: The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'GroupJoin'.