聚合操作执行数学的运算,如平均数、合计、总数、最大值、最小值

Method Description
Aggregate 在集合上执行自定义聚集操作
Average 求平均数
Count 求集合的总数
LongCount 求集合的总数
Max 最大值
Min 最小值
Sum 总数
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接受2个参数,一般第一个参数是称为累积数(默认情况下等于第一个值),而第二个代表了下一个值。第一次计算之后,计算的结果会替换掉第一个参数,继续参与下一次计算。

public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source,
TAccumulate seed,
Func<TAccumulate, TSource, TAccumulate> func);

seed作为种子值进行累加

// Student collection
IList<Student> studentList = new List<Student>>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Moin", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
}; string commaSeparatedStudentNames = studentList.Aggregate<Student, string>(
"Student Names: ", // seed value
(str, s) => str += s.StudentName + "," ); Console.WriteLine(commaSeparatedStudentNames);

第三个参数对结果进行构造返回

public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source,
TAccumulate seed,
Func<TAccumulate, TSource, TAccumulate> func,
Func<TAccumulate, TResult> resultSelector);
IList<Student> studentList = new List<Student>>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Moin", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
}; string commaSeparatedStudentNames = studentList.Aggregate<Student, string,string>(
String.Empty, // seed value
(str, s) => str += s.StudentName + ",", // returns result using seed value, String.Empty goes to lambda expression as str
str => str.Substring(,str.Length - )); // result selector that removes last comma Console.WriteLine(commaSeparatedStudentNames);

最新文章

  1. MooseFS学习-概述
  2. web组件
  3. Integrating JAD decompiler into JDeveloper(转)
  4. Search a 2D Matrix | &amp; II
  5. 实验八--uart
  6. vojis1523 NOI2002 贪吃的九头龙
  7. C# 1作业 2广场砖面积 护栏长度
  8. Myeclipse 常用操作(待补充)
  9. List&lt;bean&gt;转换Jsonarray知识心得
  10. 我的第一个spring_boot项目
  11. Docker应用:Kubernetes(容器集群)
  12. TP5.x——update更新成功但是返回是0
  13. JavaFX 简介
  14. Java编程基础篇第六章
  15. MP实战系列(一)之入门框架搭建和使用
  16. I - 取石子游戏
  17. uvm设计分析——field automation
  18. BeautifulSoup 模块详解
  19. dp——poj1088(Description)
  20. [leetcode]449. Serialize and Deserialize BST序列化反序列化二叉搜索树(尽量紧凑)

热门文章

  1. java之JDK动态代理
  2. 连接redis失败,关闭防火墙即可
  3. vi编辑器命令大全
  4. IE浏览器下,输入框最后不显示X
  5. SpringMvc入门教程
  6. centos6下手工编译vitess
  7. nginx proxy_pass 里的”/”
  8. Selenium+Python :WebDriver设计模式( Page Object )
  9. EasyUI入门视频教程
  10. Linux中的关机