2016-02-12 97 views
0

我有一個項目在C#像這樣的列表列求和:LinQ在GROUPBY乘C#

Rid   Pdv  Total Quantity 
DESAYUNOS VITP 55  1 
CENAS  REST 38  2 
COMIDAS  VITP 23  3 
DESAYUNOS REST 116  7 
CENAS  VITP 112  1 
COMIDAS  REST 11  4 
DESAYUNOS VITP 67  9 
CENAS  REST 20  2 
COMIDAS  VITP 47  5 
DESAYUNOS REST 5   2 
CENAS  VITP 10  1 

我想在至極的結果的數量乘以總並通過總和所有總數所有diferent「PDV」和「擺脫」,使用的GroupBy但它只總計每個diferent總IM「RID」和「PDV」

@foreach (var item in Model.GroupBy(l => l.Rid) 
    .Select(z => new 
        { 
         Turno = z.Key, 
         Total = z.Sum(l => l.Total) 
        })) 
{ 
    <input value="@item" /> 
} 

我怎樣才能使它「總計」乘以「數量」 Sum之前呢?

回答

1

你應該做的是這樣的:

@foreach (var item in Model 
        .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) 
        })) 
{ 
    <input value="@item" /> 
} 
0

我想在至極的結果的數量乘以總並通過總和所有diferent所有總量「PDV」和「 Rid

@foreach (var item in Model 
        .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) 
        })) 
{ 
    <input value="@item" /> 
}