Search code examples
c#linqentity-frameworklinq-to-entitiesleft-join

Entity framework left join


How do I change this query so it returns all u.usergroups?

from u in usergroups
from p in u.UsergroupPrices
select new UsergroupPricesList
{
UsergroupID = u.UsergroupID,
UsergroupName = u.UsergroupName,
Price = p.Price
};

Solution

  • adapted from MSDN, how to left join using EF 4

    var query = from u in usergroups
                join p in UsergroupPrices on u.UsergroupID equals p.UsergroupID into gj
                from x in gj.DefaultIfEmpty()
                select new { 
                    UsergroupID = u.UsergroupID,
                    UsergroupName = u.UsergroupName,
                    Price = (x == null ? String.Empty : x.Price) 
                };