【发布时间】:2021-09-27 12:22:22
【问题描述】:
我正在尝试在 c# .netcore 2.2 应用程序的 linq 中编写一个包含多个左连接的查询。我已经尝试在 linq 中编写查询,但它没有正确检索所有行。我正在尝试转换的查询如下。
select ISNULL(b.Id, '00000000-0000-0000-0000-000000000000') as 'Bat Id', p.Id as 'ProductId', p.RP, u.UnitName as 'UnitName', ISNULL(b.QTY,0) as 'BusQty', p.[Name] as 'Product Name'
from Products p left join Bat b
ON p.Id = b.ProductId
left join Units u on p.UOId = u.Id;
我到目前为止的linq
var allProducts = (from p in _db.Products
join s in _db.Bat on p.Id equals s.ProductId into ps
from s in ps.DefaultIfEmpty()
join u in _db.Units on p.UOId equals u.Id
select new
{
BatId = s.Id == null ? Guid.NewGuid() : s.Id,
RP = p.RP,
BusQty = s.QTY == null ? 0 : s.QTY,
ProductName = p.Name,
UnitName = u.UnitName,
ProductId = p.Id,
}).ToList();
【问题讨论】:
-
到目前为止你有什么 LINQ 代码?
-
@Charlieface 将我的 linq 添加到问题中
标签: c# sql linq asp.net-core sql-to-linq-conversion