我们有以下查询给我们一个左外连接:左外连接的结果发行使用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