Examples of a left outer join in LINQ

The following examples implement a left outer join of Customer with Order objects with default values
supplied to display Customer objects that don ’ t have corresponding Order objects:

Order emptyOrder = new Order { OrderID = 0, CustomerID = “XXXXX”, EmployeeID = 0,
OrderDate = DateTime.Parse(1/1/1900), RequiredDate = DateTime.Parse(1/1/1900),
ShipVia = 0, Freight = 0M, ShipName =Default Order” };
 
var query11 = from c in CustomerList
                    where c.Country == “USA”
                    join o in OrderList on c.CustomerID equals o.CustomerID into co
                    from d in co.DefaultIfEmpty(emptyOrder)
                    select new { c, d };
 
foreach (var c in query11)
sbResult.Append(String.Format(“CustID = {0}, Name = {1}, OrderID = {2},
OrderDate = {3:d}\r\n”, c.c.CustomerID, c.c.CompanyName, c.d.OrderID,
c.d.OrderDate));

Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

Filed Under: Join

Tags:

RSSComments (0)

Trackback URL

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.