我试图获得可查询
var result = _context.Product
.Include(a => a.ProductCategory)
.AsQueryable();
使用EF Core生成此查询的结果。
SELECT Prod.ProductId,
Prod.ProductName,
Prod.ProductCategoryId,
ISNULL(SUM(Inv.Quantity), 0) AS 'Qty'
FROM PRODUCTS Prod
LEFT JOIN InventoryProductDetails Inv ON Inv.ProductId = Prod.ProductId
WHERE Prod.Active = 'true'
GROUP BY Prod.ProductId,Prod.ProductName,Prod.ProductCategoryId
答案 0 :(得分:2)
_context.Product.Where(p => p.Active)
.GroupJoin(_context.InventoryProductDetails,
p => p.ProductId, inv => inv.ProductId,
(p, invs) => new {
p.ProductId,
p.ProductName
p.ProductCategoryId,
Qty = invs.Sum(i => i.Quantity)
})