Monthly Archives: June 2008

LINQ Left Join with Where on Both Tables

I struggled with this one for a bit, but here’s how you do a LINQ “LEFT JOIN” equivalent while applying WHERE to both tables and maintaining an “all” relationship on the first table:

var TeamAllocations = from team in db.Teams
where team.CustomerId == PageContext.CustomerId
join a in
(from x in db.TeamAssetAllocations where x.AssetId == _AssetId select x) on team.Id equals a.TeamId into ta
from a in ta.DefaultIfEmpty()
select new { team.Id, team.Name, a.QuantityAllowed };

Personally, I don’t really get it, especially the “from a in ta.DefaultIfEmpty()” part, but whatever.