使用 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();
问题是,如果左表(帐户)包含具有相同帐户代码值的多行,则结果集包含重复的第一行 - 因此第二行具有唯一的组合帐户代码、源系统和邮件地址字符串被“覆盖”。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊,那好多了。左连接对我来说看起来很漂亮……但一切都不适合我。
假设我有一个 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.
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.