I came across a very annoying "issue" with LINQ joins (left joins) in Entity Framework where the compiler drove me nuts!
The original query
Basically I had a base query like this:
var query = dbContext.Entity.Where(...);
var result = from e in query
join j in dbContext.JoinEntity on new { e.Id, e.OtherId } equals new { j.WId, j.OtherId } into group
from g in group.DefaultIfEmpty()
select new { e, g };
This query leads to a nasty compiler error:
The type arguments cannot be inferred from the query. Candidates are: ... (very long text here)
The solution
The problem is that I used multiple columns as join and ALL of them have to have the same name, otherwise the compiler will not be able to infer the type arguments (???). So the easy fix is something along the lines of:
var result = from e in query
join j in dbContext.JoinEntity on new { WId = e.Id, OtherId= e.OtherId } equals new { WId = j.WId, OtherId = j.OtherId } into group
from g in group.DefaultIfEmpty()
select new { e, g };
So both columns have to have the same name in the anonymous types. And yes, I could have just write plain SQL (which I also do), but that is besides the point.