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

发布于 2024-09-04 00:06:14 字数 1549 浏览 7 评论 0原文

我们有以下查询来为我们提供左外连接:

(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();

问题是,如果左表(帐户)包含具有相同帐户代码值的多行,则结果集包含重复的第一行 - 因此第二行具有唯一的组合帐户代码、源系统和邮件地址字符串被“覆盖”。

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 语句是否做错了什么?

谢谢

We have the following query to give us a left outer join:

(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();

The issue is that if the left table (accounts) contains multiple rows with the same accountcode value, the result set contains the first row duplicated - so the second row with it's unique combination of accountcode, sourcesystem and mailingaddressstring is "overwritten".

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

Are we doing something wrong with the select statement?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2024-09-11 00:06:14

啊,那好多了。左连接对我来说看起来很漂亮……但一切都不适合我。

  • 这些列中的任何(或全部)是主键吗?
  • 数据上下文的生命周期是怎样的?以前用过查询吗?以前用过它来保存记录吗?

假设我有一个 Order 记录,其 OrderId 设置为 dbml 中的主键(但不在数据库中,允许创建重复记录)。如果我要查询订单,并且 OrderID = 5 出现两次...当数据上下文看到第一个具有 OrderID 的实例时,它就会开始跟踪它。当它看到第二个实例时,它不会对行进行水化,而是返回已返回的 ID=5 的实例。

如果我的查询结果是匿名类型,我不会看到此行为,因为匿名类型在 dbml 中没有主键,并且不会被数据上下文跟踪。

Ah, well that's rather much better. The left join looks just peachy to me... but all does not sit well with me.

  • Are any (or all) of these columns the primary key?
  • What is the lifecycle of the datacontext? Has it been used to query before? Has it been used to save records before?

Suppose I have an Order record with an OrderId set as primary key in the dbml (but not in the database, allowing duplicate records to be created). If I were to query for Orders, and OrderID = 5 is in there twice... when the datacontext sees the first instance with OrderID, it starts tracking it. When it sees the second instance, instead of hydrating the row, it returns the instance it already returned with ID=5.

If my query result is an anonymous type, I wouldn't see this behavior, as the anonymous type has no primary key in the dbml and is not tracked by the datacontext.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文