如何在实体框架查询中使用左连接?

时间:2014-02-13 08:07:00

标签: c# asp.net-mvc linq entity-framework

我正在使用实体框架4.5。我必须将SQL查询转换为实体查询:

SELECT Customer.CustCode, Invoice.InvoiceId, Invoice.BatchNumber, Invoice.InvoiceDate, Invoice.AdjustFlag, Invoice.InvoiceAmount,
       Invoice.InvoiceNote, Invoice.AmountPaid, Customer.BillingContact, Customer.BillingCompany, Customer.BillingStreet1,
       Customer.BillingStreet2, Customer.BillingCity, Customer.BillingState, Customer.BillingZip, [Order].PickupDate, [Order].OrderNumber,
       [Order].OrderTotal, [Order].ProNumber, [Order].PickupCompany, [Order].PickupCity, [Order].PickUpState, [Order].Dcompany,
       [Order].Dcity, [Order].Dstate, CONVERT(varchar(5), DeliverInTime, 114) AS DelInTime, [Order].PiecesWeight1, [Order].BaseRATE,
       [Order].POD, [Order].Requester, [Order].Po1, [Order].Po2, AccessorialCharge.Description,
       OrderDriverExtraCharge.AccessorialChargeDesc, OrderDriverExtraCharge.AccessorialChargeAmount, [Order].NormalDiscount,
       - 1 * [Order].DISCAmount AS DISCAmount
FROM (((Invoice INNER JOIN
       [Order] ON Invoice.InvoiceId = [Order].InvoiceId) INNER JOIN
       Customer ON Invoice.CustID = Customer.CustID) LEFT JOIN
       OrderDriverExtraCharge ON [Order].OrderNumberId = OrderDriverExtraCharge.OrderNumberId) LEFT JOIN
       AccessorialCharge ON OrderDriverExtraCharge.AccessorialChargeId = AccessorialCharge.AccessorialChargeId
where Invoice.InvoiceId = '1117782' 

如果我将OrderDriverExtraCharge.OrderNumberId) LEFT JOIN更改为OrderDriverExtraCharge.OrderNumberId) JOIN(简单联接)或内部联接,则表示未显示正确的结果。

我试过这个:

from I in db.Invoices
join O in db.Orders on I.InvoiceId equals O.InvoiceId
join C in db.Customers on I.CustId equals C.CustId
join OD in db.OrderDriverExtraCharges on O.OrderNumberId equals OD.OrderNumberId
join AC in db.AccessorialCharges on OD.AccessorialChargeId equals AC.AccessorialChargeId
where I.InvoiceId == invoice.InvoiceId
select new PrintInvoiceViewModel()

但它没有显示所需的结果。请帮助我,如果它适合我​​,我会标记你的答案。谢谢。

2 个答案:

答案 0 :(得分:5)

你可以这样做:

from I in db.Invoices
      join O in db.Orders on I.InvoiceId equals O.InvoiceId
      join C in db.Customers on I.CustId equals C.CustId
      from OD in db.OrderDriverExtraCharges
        .Where(w=>w.OrderNumberId==O.OrderNumberId).DefaultIfEmpty()
      from AC in db.AccessorialCharges 
        .Where(w=>w.AccessorialChargeId==OD.AccessorialChargeId).DefaultIfEmpty()
      where I.InvoiceId == invoice.InvoiceId
      select new PrintInvoiceViewModel()

答案 1 :(得分:1)

您应该使用DefaultIfEmpty方法,如果序列为空,则返回IEnumerable<T>的元素或默认值的单例集合:

from I in db.Invoices
join O in db.Orders on I.InvoiceId equals O.InvoiceId
join C in db.Customers on I.CustId equals C.CustId
join OD in db.OrderDriverExtraCharges on O.OrderNumberId equals OD.OrderNumberId into ODs 
from OD in ODs.DefaultIfEmpty()
join AC in db.AccessorialCharges on OD.AccessorialChargeId equals AC.AccessorialChargeId into ACs 
from AS in ACs.DefaultIfEmpty()
where I.InvoiceId == invoice.InvoiceId
select new PrintInvoiceViewModel()