GroupBy
用于分组,组中每个元素是一个集合。
Select
用于映射,把源集合转换成目标集合[=Map]。
Aggregate
用于累积,将集合各元素累积成一个值。
注:
Select
并不是用于过滤,FindAll
才用于过滤[=Filter]。
例如:
var testList = new List<Test>
{
new Test {Key = "0", Value = 1},
new Test {Key = "1", Value = 2},
new Test {Key = "0", Value = 3},
new Test {Key = "1", Value = 4},
new Test {Key = "0", Value = 5}
};
//LINQ 隐式表达式
(
from i in testList
group i by i.Key into groups
select groups.Aggregate(0, (current, item) => current + item.Value)
)
.ToList()
.ForEach(Console.WriteLine);
//LINQ lambda表达式
testList
.GroupBy(item => item.Key)
.Select(items => items.Aggregate("", (current, item) => current + item.Value))
.ToList()
.ForEach(Console.WriteLine);