在linq中使用group by

时间:2014-01-31 16:23:46

标签: c# linq

我的列表包含factorIdamountremain列。

我需要计算按amount

分别分组的总和remainfactorId

2 个答案:

答案 0 :(得分:2)

var groupedList = myList.GroupBy(i => i.factorId);
var results = groupedList.Select(g => new { SumAmount = g.Sum(i => i.Amount), SumRemains = g.Sum(i => i.Remains) });

答案 1 :(得分:2)

在一行中:

var groupedList = myList
    .GroupBy(i => i.factorId)
    .Select(g => new 
    { 
        FactorId = g.Key, 
        SumAmount = g.Sum(i => i.Amount), 
        SumRemains = g.Sum(i => i.Remains) 
    });