0

我在 c# 中创建了一个列表,以在 html 中按类别显示总值。Infelizmente, ele está dobrando os totais。

我搜索并没有找到我尝试如下的任何答案:

@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>

    <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>
}

重复显示: 在此处输入图像描述

图片中的示例代码,此示例也在代码中。 在此处输入图像描述

4

2 回答 2

0

您可以使用Distinct()Learn More from LINQ 获取唯一记录,也可以将 span 元素移出 foreach。您可能只需要每一行的表格。

@foreach (var item in agrupar.Distinct())
{
    <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>
}
    <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 == agrupar.Distinct().idCategoria).Sum(s => s.vSubTotal).ToString("N2"); }
            @totalPorCategoria
        </span>
    </span>
于 2021-09-23T19:29:38.573 回答
0

我认为您正在寻找 GroupBy。

像这样使用它:

 var groups = Model.Items.GroupBy(t=>t.idCategoria)
 Then
 foreach(var group in groups)
{
   @group.(t=>t.vSubTotal).Sum() // gives total for each subcategory
   @group.Key // Gives the string value of the group key
}
于 2021-09-23T20:24:55.960 回答