我們有以下查詢給我們一個左外連接:左外連接的結果發行使用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();
的問題是,如果左表(賬戶)包含具有相同accountcode多行值,結果集包含第一行重複 - 因此第二行與帳戶代碼,sourcesystem和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語句時出錯了嗎?
謝謝
給定的組合是否有單個地址或多個地址?因爲連接會產生可能的匹配。如果您有一個擁有三個地址的帳戶,則查詢的結果將爲該帳戶的三行。關於您的查詢語法沒有任何不正確,但您可能需要以不同的方式處理問題。 – 2010-06-04 00:44:42
如果我瞭解你,每一行都有帳號,源系統和mailingaddressstring的唯一組合。這只是LINQ結果集,其中副本出現在似乎只基於accountcode列的地方。 – 6footunder 2010-06-04 01:47:50
此外,我們可以刪除插入後的所有代碼到t1_join中,並簡單替換爲「select t0」。所以其他條款都不影響結果。 有趣的是,在LinqPad,如果我們再嘗試: 選擇新{C1 = t0.new_accountcode,C2 = t0.new_sourcesystem,C3 = new_mailingaddressstring} 然後問題消失。這不能解決我們的問題,因爲在運行時我們不能使用匿名類型,我們也不能選擇一個新的Account對象(選擇新的Account {...})! – 6footunder 2010-06-04 01:52:04