How to implement left join in JOIN Extension method
Solution 1:
Normally left joins in LINQ are modelled with group joins, sometimes in conjunction with DefaultIfEmpty
and SelectMany
:
var leftJoin = p.Person.Where(n => n.FirstName.Contains("a"))
.GroupJoin(p.PersonInfo,
n => n.PersonId,
m => m.PersonId,
(n, ms) => new { n, ms = ms.DefaultIfEmpty() })
.SelectMany(z => z.ms.Select(m => new { n = z.n, m }));
That will give a sequence of pairs (n, m) where n
is the entry from p.Person
and m
is the entry from p.PersonInfo
, but m
will be null if there are no matches.
(It's completely untested, btw - but should give you the idea anyway :)
Solution 2:
For Left outer Join try Following query. This is tested
var leftJoin = Table1
.GroupJoin(
inner: Table2,
outerKeySelector: t1 => t1.Col1,
innerKeySelector: t2 => t2.Col2,
resultSelector: ( t1, t2Rows ) => new { t1, t2Rows.DefaultIfEmpty() }
)
.SelectMany( z =>
z.t2Rows.Select( t2 =>
new { t1 = z.t1, t2 = t2 }
)
);