blog.mha.dk
The on-line blog of Michael Holm Andersen

Left Outer Join using LINQ

Saturday, 5 April 2008 18:18 by mha

Often we need to do a left/right outer join. A friend of mine asked me how to do this using LINQ - and the short answer is this:

from s in Suppliers
    join c in Customers on s.City equals c.City into temp
    from t in temp.DefaultIfEmpty()
    select new {
    SupplierName = s.CompanyName,
    CustomerName = t.CompanyName,
    City = s.City
    }

Using LINQPad, simply make a connection to the Northwind database and execute the above query.

The "CustomerName" column will contain NULL for the rows in the Suppliers table, where no Customers exists for that specific city.

 

Comments are closed