[C#] LINQ中的GroupBy,Select,Aggregate

GroupBy用于分组,组中每个元素是一个集合。
Select用于映射,把源集合转换成目标集合[=Map]
Aggregate用于累积,将集合各元素累积成一个值。

注:
Select并不是用于过滤,FindAll才用于过滤[=Filter]

例如:

var testList = new List
{
    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);

你可能感兴趣的:([C#] LINQ中的GroupBy,Select,Aggregate)