Friday, September 11, 2009

LINQ left join

Finally this site is back!

More LINQ (in Jeremy Clarkson's voice - POWER!!!!)

In the attempt of making a left join in LINQ with my recent work, I had to troll through the web and found the following from Naveen's blog


Essentially, in its most generic form of a LINQ Left Join is something like below


var LeftJoin = from linqObj1 in List1
join linqObj2 in List2
on linqObj1.Field1 equals linqObj2.Field1 into JoinedList1List2
from linqObj2 in JoinedList1List2t.DefaultIfEmpty()
select new
{
RequiredField1 = linqObj1.RequiredField22,
RequiredField2 = linqObj2!= null ? linqObj2.RequiredField45 : null
};