[英]Left Outer Join Result Issue Using Linq to Sql
我們有以下查詢為我們提供左外部連接:
(from t0 in context.accounts
join t1 in context.addresses
on new { New_AccountCode = t0.new_accountcode, New_SourceSystem = t0.new_sourcesystem, New_Mailing = t0.new_MailingAddressString }
equals new { New_AccountCode = t1.new_AccountCode, New_SourceSystem = t1.new_SourceSystem, New_Mailing = t1.new_MailingAddressString } into t1_join
from t1 in t1_join.DefaultIfEmpty()
where
t0.statecode != 1 &&
t0.statuscode != 2 &&
t1.new_AccountCode == null &&
t1.new_SourceSystem == null &&
t1.new_MailingAddressString == null
select t0)
.OrderBy(o => o.new_accountcode)
.ThenBy(o2=>o2.new_sourcesystem)
.Skip(recordsProcessed)
.Take(recordBatchSize).ToList();
問題是,如果左側表(帳戶)包含具有相同帳戶代碼值的多行,則結果集包含重復的第一行-因此具有帳戶代碼,源系統和mailingaddressstring的唯一組合的第二行被“覆蓋”。
Given:
accounts
accountcode sourcesystem mailingaddressstring
10025 ss1 12345
10025 ss2 67891
addresses
accountcode sourcesystem mailingaddressstring
10025 ss1 12345
10025 ss2 67891
we get:
accountcode sourcesystem mailingaddressstring
10025 ss1 12345
10025 ss1 12345
我們對select語句做錯了嗎?
謝謝
嗯,那好多了。 左連接對我來說看起來像桃子一樣……但所有人的感覺都不佳。
假設我有一個Order記錄,在dbml中有一個OrderId設置為主鍵(但是在數據庫中沒有,因此可以創建重復的記錄)。 如果我要查詢訂單,並且其中有兩次OrderID = 5 ...當數據上下文看到具有OrderID的第一個實例時,它將開始對其進行跟蹤。 當看到第二個實例時,它不給行充水,而是返回已經返回ID = 5的實例。
如果我的查詢結果是匿名類型,那么我將不會看到此行為,因為該匿名類型在dbml中沒有主鍵,也不會被datacontext跟蹤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.