繁体   English   中英

使用Linq to Sql的左外部联接结果问题

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM