我正在尝试在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
}
答案 0 :(得分:0)
在我使用的查询中,这是一个愚蠢的错误:
where receipt.PONumber == 4707
为null,因此没有返回任何结果。
我已经解决了这个问题:
where receipt.PONumber == null || receipt.PONumber == 4707
我也把这个总和作为一个int。
TotalReceived = (int?)g.Sum (x => x.receipt.QuantityReceived),
QuantityPosted = (int?)g.Where (x => x.receipt.Posted == true).Sum (x => x.receipt.QuantityReceived),