0

我正在尝试用 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
}
4

1 回答 1

0

在我使用的查询中,这是一个愚蠢的错误:

where receipt.PONumber == 4707

这是空的,因此不返回任何结果。

我已经解决了这个问题:

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),
于 2013-12-14T02:36:21.153 回答