我希望将此join操作转换为foreach,因为只有当Incoming product的materialId和depotId与outgoing product的materialId和depotId匹配时,此操作才起作用。但是,如果没有相同物料和仓库id的任何出库产品和入库产品,我希望只显示入库产品的数量。因此,我应该执行一次检索,循环incomingProductTotals并检索其匹配的出库合计,以完成<代码>D9中的其余部分。但我不能。
var incomingProductTotals = Model.IncomingProduct
.GroupBy(x => new { x.depotId, x.materialId })
.Select(g => new
{
g.Key.materialId,
g.Key.depotId,
total = g.Sum(t => t.amount)
});
// retrieve all outgoing product totals (with materialId, depotId and total)
var outgoingProductTotals = Model.OutgoingProduct
.GroupBy(x => new { x.depotId, x.materialId })
.Select(g => new
{
g.Key.materialId,
g.Key.depotId,
total = g.Sum(t => t.amount)
});
var totals = incomingProductTotals
.Join(
outgoingProductTotals,
incoming => new { incoming.materialId, incoming.depotId },
outgoing => new { outgoing.materialId, outgoing.depotId },
(incoming, outgoing) => new
{
incoming.materialId,
incoming.depotId,
Total = incoming.total - outgoing.total
});
foreach (var item in totals)
{
<tr>
<td> @item.materialId </td>
<td> @item.depotId </td>
<td> @item.Total</td>
</tr>
}发布于 2019-02-25 19:10:58
var incomingProductTotals = Model.IncomingProduct
.GroupBy(x => new { x.depotId, x.materialId })
.Select(g => new
{
g.Key.materialId,
g.Key.depotId,
total = g.Sum(t => t.amount)
});
// retrieve all outgoing product totals (with materialId, depotId and total)
var outgoingProductTotals = Model.OutgoingProduct
.GroupBy(x => new { x.depotId, x.materialId })
.Select(g => new
{
g.Key.materialId,
g.Key.depotId,
total = g.Sum(t => t.amount)
});
foreach(var inProduct in incomingProductTotals)
{
var outProduct = outgoingProductTotals.where(p => p.materialId == inProduct.materialId && p.depotId == inProduct.depotId).FirstOrDefault();
if(outProduct != null)
{
<tr>
<td> @inProduct.materialId </td>
<td> @inProduct.depotId </td>
<td> @(inProduct.Total - outProduct.Total)</td>
</tr>
}
}https://stackoverflow.com/questions/54864739
复制相似问题