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; }
Here's an example if you need to join more than 2 tables:
Ref: https://stackoverflow.com/a/17142392/2343
OUTPUT
take look at this example
now you are able to
include elements from the left
even if that elementhas no matches in the right
, in our case we retrivedArlene
even he has no matching in the righthere is the reference
How to: Perform Left Outer Joins (C# Programming Guide)
This is a SQL syntax compare to LINQ syntax for inner and left outer joins. Left Outer Join:
http://www.ozkary.com/2011/07/linq-to-entity-inner-and-left-joins.html
"The following example does a group join between product and category. This is essentially the left join. The into expression returns data even if the category table is empty. To access the properties of the category table, we must now select from the enumerable result by adding the from cl in catList.DefaultIfEmpty() statement.
There are three tables: persons, schools and persons_schools, which connects persons to the schools they study in. A reference to the person with id=6 is absent in the table persons_schools. However the person with id=6 is presented in the result lef-joined grid.
Here is a fairly easy to understand version using method syntax: