且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

LINQ左连接不返回与T-SQL左连接相同

更新时间:2023-01-20 11:38:12

检查这些:

使用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)[^]