2014-02-13 108 views
1

我正在使用實體框架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 JOINOrderDriverExtraCharge.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() 

但它沒有顯示所需的結果。請幫助我,我會標記你的答案,如果它對我有用。謝謝。

+1

查看此鏈接了。它包含一個用於左外連接的示例。 http://itworksonmymachine.wordpress.com/2009/09/27/sql-to-linq-cheat-sheet/ – samar

回答

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

你應該使用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()