Home > OS >  How to remove duplicate value from a C# foreach?
How to remove duplicate value from a C# foreach?

Time:09-24

I created a list in c# to display in html the total values by category. Infelizmente, ele está dobrando os totais.

I searched and didn't find any answer I tried as follows:

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

Duplicate displaying: enter image description here

Sample code in image, this example is above in code too. enter image description here

CodePudding user response:

I think you are looking for GroupBy.

Use it like this:

 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
}

CodePudding user response:

You can use Distinct()Learn More from LINQ to get unique records, and also move out the span element outside of foreach. You probably just need table inside for each row.

@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>
  •  Tags:  
  • c#
  • Related