首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用foreach而不是Join函数?

如何使用foreach而不是Join函数?
EN

Stack Overflow用户
提问于 2019-02-25 19:02:57
回答 1查看 53关注 0票数 0

我希望将此join操作转换为foreach,因为只有当Incoming productmaterialIddepotIdoutgoing productmaterialIddepotId匹配时,此操作才起作用。但是,如果没有相同物料和仓库id的任何出库产品和入库产品,我希望只显示入库产品的数量。因此,我应该执行一次检索,循环incomingProductTotals并检索其匹配的出库合计,以完成<代码>D9中的其余部分。但我不能。

代码语言:javascript
复制
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>
            }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-25 19:10:58

代码语言:javascript
复制
        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>
             }

         }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54864739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档