LINQ: Left Join

A left join is when each item in the first collection is returned regardless of whether it has any related items in the second collection. For instance, say you want to find out how many orders were made for each product in your store.  You also want to see the products that did not have any orders so that you could decide whether to discontinue those items.  You can achieve this by a left outer join on products to order details using the ProductID key.   Query Syntax var list = from prod in Products join od in OrderDetails on prod.ProductID equals od.ProductID into orderDetailList from odl in orderDetailList.DefaultIfEmpty() select new { id = prod.ProductID, name=prod.ProductName, quantity=odl.Quantity ?? 0 };   Lambda Expression var orders = Products .GroupJoin(OrderDetails, p => p.ProductID, d => d.ProductID, (p, od) => new {p, … [Read more...]