左外连接的 Linq 查询无法正常工作
Linq query of left outer join not properly working
我将 sql 查询转换为 linq 查询,没有任何 错误 。
现在,我的问题是我在 sql 查询中正确获取数据,而在 linq 查询中显示整个数据而不过滤 product 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语法或查询有问题?
尝试使用这个查询
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();
检查以下查询 return 没有产品
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 }
我将 sql 查询转换为 linq 查询,没有任何 错误 。
现在,我的问题是我在 sql 查询中正确获取数据,而在 linq 查询中显示整个数据而不过滤 product 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语法或查询有问题?
尝试使用这个查询
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();
检查以下查询 return 没有产品
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 }