简体   繁体   中英

Linq 2 SQL Convert to Outer Join

I have this linq to sql query that features several joins. I want to convert one of them to an outer join. The problem is that this query already has a from clause. How can I convert this query to use a left join on the OrderCertification table instead of an inner join?

The following query did not work (caused my app to crash):

var orderSummaries = from os in DbOrder.QueryOrderSummaries().Where(os => orders.Contains(os.OrderID))
   join o in dc.ORDERs on os.OrderID equals o.OrderID
   join oa in dc.ORDERADDRESSes on os.OrderID equals oa.OrderID
   join d in dc.vDoctors on o.DoctorID equals d.DoctorID
   join c in dc.ORDERCERTIFICATIONs on os.OrderID equals c.OrderID into oc
   from certification in oc.DefaultIfEmpty()
   select new BatchOrderItem {
      OrderSummary = os,
      Order = o,
      ShipTo = oa,
      Prescriber = d,
      CertificationContact = certification
    };

The trick is using DefaultIfEmpty to return all the rows from the first table. Check out this example: http://msdn.microsoft.com/en-us/library/bb397895.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM