繁体   English   中英

Linq 左外连接查询无法正常工作

[英]Linq query of left outer join not properly working

我将sql查询转换为linq查询,没有任何错误

现在,我的问题是我在 sql 查询中正确获取了数据,而在 linq 查询中显示了整个数据而不过滤产品 null

这是我的代码:

SQL 查询

SELECT Name
FROM ProductMaster product
LEFT JOIN TouchWastageGroup touchWastageGroup ON touchWastageGroup.Product = product.Name and touchWastageGroup.GroupNameId = 2 and touchWastageGroup.CaratId = 6
WHERE touchWastageGroup.Product IS NULL 

从这个查询数据显示正常。

Linq 查询

var productSelected = (from product in _productMasterRepository.Table
from touchWastageGroup in _touchWastageGroupRepository.Table
.Where(touchWastageGroup => touchWastageGroup.Product == product.Name && touchWastageGroup.GroupNameId == 2 && touchWastageGroup.CaratId == 6)                                   
.DefaultIfEmpty().Where(x => x.Product == null)
select new
{
   Result = product.Name
}).ToList();

linq 的相同查询显示了整个数据而不过滤它( Where(x => x.Product == null) )。

linq语法或查询有问题吗?

检查以下查询返回没有产品

from product in _productMasterRepository.Table
join touchWastageGroup in _touchWastageGroupRepository.Table on new { Product = product.Product, GroupNameId = 2, CaratId = 6 } equals new { touchWastageGroup.Product, touchWastageGroup.GroupNameId, touchWastageGroup.CaratId } into joinedResult
from touchWastageGroup in joinedResult.DefaultIfEmpty()
where touchWastageGroup == null
select new { Result = product.Name }

尝试使用此查询

var productSelected = from product in _productMasterRepository.Table
join from touchWastageGroup in _touchWastageGroupRepository.Table on
product.Name equals touchWastageGroup.Product into temp
from t in temp.DefaultIfEmpty()
where t.GroupNameId == 2 && t.CaratId == 6
select new
{
   Result = product.Name
}).ToList();

暂无
暂无

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

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