聚合運(yùn)算符對集合中元素的數(shù)值屬性執(zhí)行數(shù)學(xué)運(yùn)算,如Average、Aggregate、Count、Max、Min和Sum。
| 方法 | 描述 |
|---|---|
| Aggregate | 對集合中的值執(zhí)行自定義聚合操作。 |
| Average | 計算集合中數(shù)字項的平均值。 |
| Count | 統(tǒng)計集合中的元素。 |
| LongCount | 統(tǒng)計集合中的元素。 |
| Max | 查找集合中的最大值。 |
| Min | 查找集合中的最小值。 |
| Sum | 計算集合中值的總和。 |
聚合方法執(zhí)行累加操作。聚合擴(kuò)展方法具有以下重載方法:
Aggregate()重載:
public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func); public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func); public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector);
下面的示例演示了 Aggregate 方法,該方法返回字符串列表中逗號分隔的元素。
IList<String> strList = new List<String>() { "One", "Two", "Three", "Four", "Five"};
var commaSeperatedString = strList.Aggregate((s1, s2) => s1 + ", " + s2);
Console.WriteLine(commaSeperatedString);One, Two, Three, Four, Five
在上面的示例中,Aggregate擴(kuò)展方法從strList集合返回逗號分隔的字符串。下圖說明了以上示例中執(zhí)行的整個聚合操作。

如上圖所示,strList“ One”的第一項將作為 s1傳遞,其余項將作為 s2傳遞。Lambda 表達(dá)式(s1,s2) = > s1 + ","+ s2將被視為 s1 = s1 +","+ s1,其中 s1將為集合中的每個項累積。因此,Aggregate 方法將返回逗號分隔的字符串。
Dim strList As IList(Of String) = New List(Of String) From {
"One",
"Two",
"Three",
"Four",
"Five"
}
Dim commaSeparatedString = strList.Aggregate(Function(s1, s2) s1 + ", " + s2)Aggregate的第二個重載方法需要第一個參數(shù)來累積種子值。第二個參數(shù)是Func類型的委托:
TAccumulate Aggregate<TSource, TAccumulate>(TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func);
下面的示例在Aggregate擴(kuò)展方法中將字符串用作種子值。
// 學(xué)生集合
IList<Student> studentList = new List<Student>>() {
new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,
new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,
new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }
};
string commaSeparatedStudentNames = studentList.Aggregate<Student, string>(
"Student Names: ", // 種子價值
(str, s) => str += s.StudentName + "," );
Console.WriteLine(commaSeparatedStudentNames);// 學(xué)生集合
Dim studentList = New List(Of Student) From {
New Student() With {.StudentID = 1, .StudentName = "John", .Age = 13},
New Student() With {.StudentID = 2, .StudentName = "Moin", .Age = 21},
New Student() With {.StudentID = 3, .StudentName = "Bill", .Age = 18},
New Student() With {.StudentID = 4, .StudentName = "Ram", .Age = 20},
New Student() With {.StudentID = 5, .StudentName = "Ron", .Age = 15}
}
Dim commaSeparatedStudentNames = studentList.Aggregate(Of String)(
"Student Names: ",
Function(str, s) str + s.StudentName + ",")
Console.WriteLine(commaSeparatedStudentNames);Student Names: John, Moin, Bill, Ram, Ron,
在上面的示例中,Aggregate 方法的第一個參數(shù)是“ Student Names: ”字符串,該字符串將與所有學(xué)生名一起累積。Lambda 表達(dá)式中的逗號將作為第二個參數(shù)傳遞。
下面的示例使用 Aggregate 運(yùn)算符添加所有學(xué)生的年齡。
// 學(xué)生集合
IList<Student> studentList = new List<Student>>() {
new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,
new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,
new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }
};
int SumOfStudentsAge = studentList.Aggregate<Student, int>(0,
(totalAge, s) => totalAge += s.Age );現(xiàn)在,讓我們看看第三個重載方法,它需要 Func 委托表達(dá)式的第三個參數(shù)作為結(jié)果選擇器,這樣您就可以公式化結(jié)果。
IList<Student> studentList = new List<Student>>() {
new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,
new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,
new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }
};
string commaSeparatedStudentNames = studentList.Aggregate<Student, string,string>(
String.Empty, // 種子值
(str, s) => str += s.StudentName + ",", // 使用種子值返回結(jié)果,String.Empty以str的形式進(jìn)入lambda表達(dá)式
str => str.Substring(0,str.Length - 1 )); // 刪除最后一個逗號的結(jié)果選擇器
Console.WriteLine(commaSeparatedStudentNames);在上面的示例中,我們指定了一個lambda表達(dá)式str => str.Substring(0,str.Length - 1 ),該表達(dá)式將刪除字符串結(jié)果中的最后一個逗號。下面是VB.Net中的相同示例。
// 學(xué)生集合
Dim studentList = New List(Of Student) From {
New Student() With {.StudentID = 1, .StudentName = "John", .Age = 13},
New Student() With {.StudentID = 2, .StudentName = "Moin", .Age = 21},
New Student() With {.StudentID = 3, .StudentName = "Bill", .Age = 18},
New Student() With {.StudentID = 4, .StudentName = "Ram", .Age = 20},
New Student() With {.StudentID = 5, .StudentName = "Ron", .Age = 15}
}
Dim commaSeparatedStudentNames = studentList.Aggregate(Of String, String)(
String.Empty,
Function(str, s) str + s.StudentName + ",",
Function(str) str.Substring(0, str.Length - 1))
Console.WriteLine(commaSeparatedStudentNames);John, Moin, Bill, Ram, Ron
C # 或 VB.Net 中的查詢語法不支持聚合運(yùn)算符。
在下一部分中了解另一種合計運(yùn)算符-Average (計算平均值)。