简体   繁体   English

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

[英]Left Outer Join Result Issue Using Linq to Sql

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". 问题是,如果左侧表(帐户)包含具有相同帐户代码值的多行,则结果集包含重复的第一行-因此具有帐户代码,源系统和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

Are we doing something wrong with the select statement? 我们对select语句做错了吗?

Thanks 谢谢

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). 假设我有一个Order记录,在dbml中有一个OrderId设置为主键(但是在数据库中没有,因此可以创建重复的记录)。 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. 如果我要查询订单,并且其中有两次OrderID = 5 ...当数据上下文看到具有OrderID的第一个实例时,它将开始对其进行跟踪。 When it sees the second instance, instead of hydrating the row, it returns the instance it already returned with ID=5. 当看到第二个实例时,它不给行充水,而是返回已经返回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. 如果我的查询结果是匿名类型,那么我将不会看到此行为,因为该匿名类型在dbml中没有主键,也不会被datacontext跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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