简体   繁体   English

LINQ中的左外部联接不起作用

[英]Left Outer Join in Linq not working

I'm trying to do a left outer join in Linq but the below code is not working 我正在尝试在Linq中进行左外部联接,但以下代码不起作用

 var result = from dataRows1 in agdt.AsEnumerable()
              join dataRows2 in hwt.AsEnumerable()
              on dataRows1.Field<string>("ID") equals dataRows2.Field<string>("HWID") 
              where ((dataRows2.Field<string>("HWID") == null) && 
                    (dataRows1.Field<string>("TYPE")=="a"))
              select dataRows1;

Without the where clauses I receive about 37000 rows and with it I receieve 0. The agdt table has 12000 rows and the hwt table has 6000. This is getting very frustrating. 没有where子句,我将收到大约37000行,并且收到agdt表具有12000行, hwt表具有6000。这令人非常沮丧。 Can someone please help? 有人可以帮忙吗?

You are missing the DefaultIfEmpty method call. 您缺少DefaultIfEmpty方法调用。

From what I understand from your query, it should look something like: 根据您对查询的了解,它应该类似于:

var result = from dataRows1 in agdt.AsEnumerable()
          join dataRows2 in hwt.AsEnumerable()
          on dataRows1.Field<string>("ID") equals dataRows2.Field<string>("HWID") 
          into groupJoin
          from leftOuterJoinedTable in groupJoin.DefaultIfEmpty()
          where (leftOuterJoinedTable == null && 
                (dataRows1.Field<string>("TYPE")=="a"))
          select dataRows1;

It seems to me that this in essence would be the same as running the following SQL query 在我看来,这实质上与运行以下SQL查询相同

SELECT DR1.* FROM DataRows1 DR1 INNER JOIN DataRows2 DR2 ON DR1.ID=DR2.HWID WHERE DR2.HWID IS NULL AND DR1.Type='a' 选择DR1。*从DataRows1 DR1内连接DataRows2 DR2在DR1.ID = DR2.HWID上DR2.HWID为NULL并且DR1.Type ='a'

Essentially your LINQ is doing an inner join and then executing the where. 本质上,您的LINQ正在执行内部联接,然后执行where。 To truly do a left join, see the link 要真正进行左联接,请参见链接

LEFT OUTER JOIN in LINQ LINQ的左外连接

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

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