How to perform left outer join in C# LINQ to objects without using join-on-equals-into
clauses? Is there any way to do that with where
clause?
Correct problem:
For inner join is easy and I have a solution like this
List<JoinPair> innerFinal = (from l in lefts from r in rights where l.Key == r.Key
select new JoinPair { LeftId = l.Id, RightId = r.Id})
but for left outer join I need a solution. Mine is something like this but it's not working
List< JoinPair> leftFinal = (from l in lefts from r in rights
select new JoinPair {
LeftId = l.Id,
RightId = ((l.Key==r.Key) ? r.Id : 0
})
where JoinPair is a class:
public class JoinPair { long leftId; long rightId; }
Using lambda expression
Necromancing.
If a database driven LINQ provider is used, a significantly more readable left outer join can be written as such:
If you omit the
DefaultIfEmpty()
you will have an inner join.Take the accepted answer:
This syntax is very confusing, and it's not clear how it works when you want to left join MULTIPLE tables.
Note
It should be noted that
from alias in Repo.whatever.Where(condition).DefaultIfEmpty()
is the same as an outer-apply/left-join-lateral, which any (decent) database-optimizer is perfectly capable of translating into a left join, as long as you don't introduce per-row-values (aka an actual outer apply). Don't do this in Linq-2-Objects (because there's no DB-optimizer when you use Linq-to-Objects).Detailed Example
When used with LINQ 2 SQL it will translate nicely to the following very legible SQL query:
Edit:
See also " Convert SQL Server query to Linq query " for a more complex example.
Also, If you're doing it in Linq-2-Objects (instead of Linq-2-SQL), you should do it the old-fashioned way (because LINQ to SQL translates this correctly to join operations, but over objects this method forces a full scan, and doesn't take advantage of index searches, whyever...):
As stated on:
101 LINQ Samples - Left outer join
I would like to add that if you get the MoreLinq extension there is now support for both homogenous and heterogeneous left joins now
http://morelinq.github.io/2.8/ref/api/html/Overload_MoreLinq_MoreEnumerable_LeftJoin.htm
example:
EDIT:
In retrospect this may work, but it converts the IQueryable to an IEnumerable as morelinq does not convert the query to SQL.
You can instead use a GroupJoin as described here: https://stackoverflow.com/a/24273804/4251433
This will ensure that it stays as an IQueryable in case you need to do further logical operations on it later.
An implementation of left outer join by extension methods could look like
The resultselector then has to take care of the null elements. Fx.
Take a look at this example. This query should work: