2016-06-09 186 views
0

我有下面的查詢中sqllite工作與左外連接做一個LEFT OUTER JOIN在LINQ查詢

 select * from customer cu 
     inner join contract cnt on cu.customerId = cnt.customerid 
     inner join address addy on cu.addressid = addy.addressId 
     inner join csrAssoc cassc on cu.customerid = cassc.customerId 
     left outer join CustomerServiceRepresentative csrr on cassc.csrid = csrr.customerservicerepresentativeId 
     inner join customerServiceManager csmm on cassc.csmid = csmm.customerservicemanagerId 
     where cu.customernumber = '22222234' 

我希望能夠在LINQ申請一個左外連接在這條線查詢低於

 join csrr in objCsrCustServRep.AsEnumerable() on cassc.CsrId equals 
            csrr.CustomerServiceRepresentativeId 

     VisitRepData = (from cu in objCustomer.AsEnumerable() 
           join cnt in objContract.AsEnumerable() on cu.customerId equals cnt.customerId 
           join addy in objAddress.AsEnumerable() on cu.addressId equals addy.addressId 
           join cassc in objCsrAssoc.AsEnumerable() on cu.customerId equals cassc.CustomerId 
           join csrr in objCsrCustServRep.AsEnumerable() on cassc.CsrId equals 
            csrr.CustomerServiceRepresentativeId 
           join csmm in objCustServMan on cassc.CsmId.ToString() equals csmm.customerServiceManagerId 
           where cu.CustomerNumber == (customernbr) 

如何在linq查詢中執行左外連接?

這是我的意見,調整和運行代碼後。另一部分也被添加。我所得到的是對象沒有設置爲對象的實例。

  var VisitRepData = from cu in objCustomer.AsEnumerable() 
           join cnt in objContract.AsEnumerable() on cu.customerId equals cnt.customerId 
           join addy in objAddress.AsEnumerable() on cu.addressId equals addy.addressId 
           join cassc in objCsrAssoc.AsEnumerable() on cu.customerId equals cassc.CustomerId 
           join csrr in objCsrCustServRep.AsEnumerable() on cassc.CsrId equals 
            csrr.CustomerServiceRepresentativeId into temp 
           from tempItem in temp.DefaultIfEmpty() 
           join csmm in objCustServMan on cassc.CsmId.ToString() equals csmm.customerServiceManagerId 
           where cu.CustomerNumber == (customernbr) 
           select new 
           { 
            cu.customerId, 
            cu.CustomerNumber, 
            cu.customerName, 
            cu.dateActive, 
            cnt.contractExpirationDate, 
            addy.street, 
            addy.street2, 
            addy.city, 
            addy.state, 
            addy.zipcode, 
            cu.EMail, 
            cu.phoneNo, 
            cu.faxNumber, 
            csmm.customerServiceManagerName, 
            tempItem.CustomerServiceRepresentativeName, 
           }; 

          foreach (var item in VisitRepData) 
          { 
           var one = item.customerId; 
           var two = item.CustomerNumber; 
          } 
+4

可能的[LINQ OUTER JOIN]中的重複(http://stackoverflow.com/questions/3404975/left-outer-join-in-linq) –

+0

沒有在您的鏈接中標記爲答案。 – user2320476

+1

對於你來說,是不是有216個upvotes來閱讀答案?沒有考慮到這個問題是非常基本的,並且缺乏基礎研究 - [join clause(C#Reference)](https://msdn.microsoft.com/zh-cn/library/bb311040.aspx),[如何執行:左外連接(C#編程指南)](https://msdn.microsoft.com/en-us/library/bb397895.aspx)等。 –

回答

0

documentation

左外連接是一個連接,其中第一集合中的每個元素被返回,而不管其是否具有所述第二集合中的任何相關的元件。 可以使用LINQ通過調用上的一組結果的DefaultIfEmpty方法執行左外連接連接

(重點煤礦)基於MSDN鏈接

以上,如果我理解您的要求正確,查詢應該是這樣的:

VisitRepData = from cu in objCustomer.AsEnumerable() 
          join cnt in objContract.AsEnumerable() on cu.customerId equals cnt.customerId 
          join addy in objAddress.AsEnumerable() on cu.addressId equals addy.addressId 
          join cassc in objCsrAssoc.AsEnumerable() on cu.customerId equals cassc.CustomerId 
          join csrr in objCsrCustServRep.AsEnumerable() on cassc.CsrId equals 
           csrr.CustomerServiceRepresentativeId into temp 
          from tempItem in temp.DefaultIfEmpty() 
          join csmm in objCustServMan on cassc.CsmId.ToString() equals csmm.customerServiceManagerId 
          where cu.CustomerNumber == (customernbr) 

具體來說,左外連接與該代碼執行:

join csrr in objCsrCustServRep.AsEnumerable() 
    on cassc.CsrId equals csrr.CustomerServiceRepresentativeId 
    into temp 
from tempItem in temp.DefaultIfEmpty() 
+0

它看起來不錯,但需要能夠拉這個領域。 csrName = csrr.CustomerServiceRepresentativeName,現在給我一個錯誤 – user2320476

+0

@ user2320476我在原始問題中沒有看到引用這個字段 - 很難在不看到整個圖片的情況下將我的答案修改爲特定的代碼;然而,左外連接應該工作,並且你應該能夠適應你的情況。 – CoolBots

+0

謝謝CoolBots。我添加了代碼的最後一部分,並且所有正在獲取的對象都未設置爲對象的實例。 – user2320476