Skip to content Skip to sidebar Skip to footer

Linq Groupby Multiply Columns And Sum C#

i have a list of items in c# like this: Rid Pdv Total Quantity DESAYUNOS VITP 55 1 CENAS REST 38 2 COMIDAS VITP 23 3 DESAYUNO

Solution 1:

You should do it like this:

@foreach (var item inModel
                     .Select(x =>new//here you count your total
                     { 
                         Rid = x.Rid, 
                         Total = x.Total * x.Quantity
                     })
                     .GroupBy(l => l.Rid) //and then grouping
                     .Select(z =>new 
                     { 
                         Turno = z.Key, 
                         Total = z.Sum(l => l.Total) 
                     }))
{
   <inputvalue="@item" />
}

Solution 2:

I want to get a result in wich the Quantity is multiply by the Total and by that sum all totals for all diferent "Pdv" and "Rid"

@foreach (var item inModel
                     .Select(m =>new { Rid = m.Rid, m.Pdv, Total =m.Total * m.Quantity})
                     .GroupBy(g =>new { g.Rid, g.Pdv} )
                     .Select(s =>new 
                     { 
                         Rid = s.Key.Rid, 
                         Pdv = s.Key.Pdv, 
                         Total = s.Sum(t => t.Total) 
                     }))
{
   <inputvalue="@item" />
}

Post a Comment for "Linq Groupby Multiply Columns And Sum C#"