LINQ左连接不返回与T-SQL左连接相同 [英] LINQ Left join not returning the same as T-SQL left join
本文介绍了LINQ左连接不返回与T-SQL左连接相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我正在研究这个解决方案,我想将以下T-SQL转换为linq查询
选择
SI.Spreadpublicationcode
,SI.spreadpage
,SI.shotsequence
,si.itemnumbershort
来自 #HacksTemp hck
left join #TempSource SI on
hck.Publication = SI.Spreadpublicationcode AND
hck.Page = SI.spreadpage AND
hck.Shot = SI.shotsequence and
hck.ItemNumberShort = SI.ItemNumberShort
其中 si.spreadpublicationcode null
我所做的是
var diff = from c in check
在源代码中加入新的{c.ItemNumberShort,c.Page,c.Shot,c.Publication}等于new {s.ItemNumberShort, s.Page,s.Shot,s.Publication}
从di.DefaultIfEmpty()中的s到di
其中s == null
select s;
返回diff.ToList< Hack>();
但是我的输出非常不同T-sql我得到了39行,但在我的linq查询中,它返回完整的检查,即8780原始数据,我使用的数据都是相同的
如果有人的话用我在LINQ中错过的东西纠正我
谢谢
解决方案
检查这些:
使用LINQ进行左连接,右连接 [ ^ ]
如何:执行左外连接(C#编程指南) ) [ ^ ]
Hi
I am working on a this solution where i wanted to convert the following T-SQL to linq query
Select
SI.Spreadpublicationcode
,SI.spreadpage
,SI.shotsequence
,si.itemnumbershort
from #HacksTemp as hck
left join #TempSource SI on
hck.Publication = SI.Spreadpublicationcode AND
hck.Page = SI.spreadpage AND
hck.Shot = SI.shotsequence and
hck.ItemNumberShort = SI.ItemNumberShort
where si.spreadpublicationcode is null
what i have done so for is
var diff = from c in check join s in source on new { c.ItemNumberShort, c.Page, c.Shot, c.Publication } equals new { s.ItemNumberShort, s.Page, s.Shot, s.Publication } into di from s in di.DefaultIfEmpty() where s == null select s; return diff.ToList<Hack>();
but the out put is very differnt on my T-sql i get 39 rows back but on my linq query it return the full check i.e 8780 raws i am using the data in both are the same
appricate if someone correnct me with what i have missed out in my LINQ
thanks
解决方案
Check these:
Left Join, Right Join Using LINQ[^]
How to: Perform Left Outer Joins (C# Programming Guide)[^]
这篇关于LINQ左连接不返回与T-SQL左连接相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文