I have 2 lists of the same type. The left list:
var leftList = new List();
leftList.Add(new Person {Id = 1, Name = \"John\", Chang
This looks like a pretty standard left outer join scenario.
I always keep this extension method handy for left outer joins so I don't have to look up how to use the nasty query syntax (or remember wtf a GroupJoin is)...
public static class LinqEx
{
public static IEnumerable LeftOuterJoin(
this IEnumerable outer,
IEnumerable inner,
Func outerKeySelector,
Func innerKeySelector,
Func resultSelector)
{
return outer
.GroupJoin(inner, outerKeySelector, innerKeySelector, (a, b) => new
{
a,
b
})
.SelectMany(x => x.b.DefaultIfEmpty(), (x, b) => resultSelector(x.a, b));
}
}
Now you can:
leftList.LeftOuterJoin(
rightList,
lft => lft.Id,
rgt => rgt.Id,
(lft, rgt) => new Person{Id = lft.Id,
Name = lft.Name,
Changed = rgt == null ? lft.Changed : rgt.Changed})