Linq的左外连接不工作
本文关键字:工作 连接 Linq | 更新日期: 2023-09-27 18:10:13
我试图在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;
没有where子句,我收到大约37000行,有了where子句,我收到0行。agdt
表有12000行,hwt
表有6000行。这让人非常沮丧。有人能帮帮我吗?
您错过了DefaultIfEmpty
方法调用。
根据我对你的查询的理解,它应该看起来像:
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;
在我看来,这在本质上与运行以下SQL查询相同
选择根据DR1。*从DataRows1 DR1内部连接DataRows2在DR1.ID = DR2.HWID在哪里DR2。Hwid为空和根据DR1。Type = ' a '
本质上你的LINQ正在做一个内部连接,然后执行where。要真正执行左连接,请参阅链接