You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决C# foreach循环中分类汇总值重复展示的问题

问题根源

当前代码的循环对象agrupar是商品维度的集合,同一分类下存在多个商品时,每个商品的循环都会触发一次分类总计的渲染,最终导致同分类的总计重复展示。

解决方案

方案1(推荐):按分类分组渲染

先将集合按idCategoria分组,遍历每个分类,先输出分类下的所有商品,最后仅输出一次该分类的总计。修改后代码如下:

@* 先按分类ID对数据分组 *@
@foreach (var categoriaGroup in agrupar.GroupBy(c => c.idCategoria))
{
    <tbody>
        @* 遍历当前分类下的所有商品 *@
        @foreach (var item in categoriaGroup)
        {
            <tr style="border:none !important">
                @if (ViewBag.bImagemProduto == true)
                {
                    if (!String.IsNullOrEmpty(item.xCaminhoImgProduto))
                    {
                        <th style="width:5% !important ; text-align:center"><img src="@item.xCaminhoImgProduto" alt="img" width="50" height="50"></th>
                    }
                    else
                    {
                        <th style="width:5% !important ; text-align:center"></th>
                    }
                }
                @if (ViewBag.bDescricaoProduto)
                {
                    <th style="width:30% !important;word-wrap: normal;word-break: break-all;"><div style="word-wrap: normal;word-break: break-word;font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;font-weight: 600 !important;font-size: 12px !important;">@item.xNome</div></th>
                }
                @if (ViewBag.bUnProduto)
                {
                    <th style="font-size: 13px !important; width:5% !important ; text-align:center !important">@item.xUnidade</th>
                }                
            </tr>
        }
    </tbody>

    @* 每个分类仅输出一次总计 *@
    <span style="margin-bottom: 1px !important; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important ; font-size: 18px !important ; color: #4CAF50 !important ; font-weight: 700 !important ; float: right !important">
        Total por categoria:
        <span style="color: #212121 !important">
            @{ var totalPorCategoria = Model.itens.Where(c => c.idCategoria == categoriaGroup.Key).Sum(s => s.vSubTotal).ToString("N2"); }
            @totalPorCategoria
        </span>
    </span>
}

提示:建议将分组逻辑提前到后端控制器中实现,避免在视图层做数据处理,提升渲染效率。

方案2:新增分类ID判断

若需要保留原有agrupar的遍历逻辑,可以新增临时变量存储上一次渲染的分类ID,当分类ID变化时才渲染总计,修改后代码如下:

@{ int lastCategoriaId = -1; }
@foreach (var item in agrupar)
{
    <tbody>
        <tr style="border:none !important">
            @* 原有商品渲染逻辑不变 *@
            @if (ViewBag.bImagemProduto == true)
            {
                if (!String.IsNullOrEmpty(item.xCaminhoImgProduto))
                {
                    <th style="width:5% !important ; text-align:center"><img src="@item.xCaminhoImgProduto" alt="img" width="50" height="50"></th>
                }
                else
                {
                    <th style="width:5% !important ; text-align:center"></th>
                }
            }
            @if (ViewBag.bDescricaoProduto)
            {
                <th style="width:30% !important;word-wrap: normal;word-break: break-all;"><div style="word-wrap: normal;word-break: break-word;font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;font-weight: 600 !important;font-size: 12px !important;">@item.xNome</div></th>
            }
            @if (ViewBag.bUnProduto)
            {
                <th style="font-size: 13px !important; width:5% !important ; text-align:center !important">@item.xUnidade</th>
            }                
        </tr>
    </tbody>

    @* 仅分类ID变化时输出总计 *@
    @if (item.idCategoria != lastCategoriaId)
    {
        <span style="margin-bottom: 1px !important; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important ; font-size: 18px !important ; color: #4CAF50 !important ; font-weight: 700 !important ; float: right !important">
            Total por categoria:
            <span style="color: #212121 !important">
                @{ var totalPorCategoria = Model.itens.Where(c => c.idCategoria == item.idCategoria).Sum(s => s.vSubTotal).ToString("N2"); }
                @totalPorCategoria
            </span>
        </span>
        lastCategoriaId = item.idCategoria;
    }
}

内容的提问来源于stack exchange,提问作者Thiago Corrêa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 20:45:03