我正在尝试用 C# 表达式编写一个 LINQ 查询,以获取采购订单的概述,其中包括一个包含采购订单TblPODetails
中项目列表的表。我想获取此表中的每个行项目,然后将已收到的项目总数添加到表中TblPOReceipts
。如果没有收到任何项目,则TblPOReceipts
显示 null 并且查询返回 0 个结果。
我怎样才能在 中允许空值TblPOReceipts
并且仍然从 中获得我的其余结果TblPODetails
?
这是我的查询:
from podetail in TblPODetails
from pricebook in TblPriceBooks.Where(x => x.ItemID == podetail.ItemID).DefaultIfEmpty()
from receipt in TblPOReceipts.Where(x => x.ItemID ==podetail.ItemID).DefaultIfEmpty()
where podetail.PONumber == 4707
where receipt.PONumber == 4707
group new { receipt, podetail, pricebook } by new {pricebook, podetail, receipt.QuantityReceived } into g
select new {
g.Key.podetail.ItemDescription,
TotalReceived = g.Sum (x => x.receipt.QuantityReceived),
QuantityPosted = g.Where (x => x.receipt.Posted == true).Sum (x => x.receipt.QuantityReceived),
g.Key.podetail.ItemID,
g.Key.podetail.QuantityOrdered,
g.Key.podetail.ProjectedCost,
g.Key.podetail.TotalProjectedCost,
g.Key.pricebook.BaseCost,
g.Key.pricebook.ItemURL
}