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

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);

LINQ 学习路程 -- 查询操作 Aggregate的更多相关文章

  1. LINQ 学习路程 -- 查询操作 Expression Tree

    表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构 ...

  2. LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending

    Sorting Operator Description OrderBy 通过给定的字段进行升序 降序 排序 OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用 Then ...

  3. LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行

    延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...

  4. LINQ 学习路程 -- 查询操作 Join

    Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join ...

  5. LINQ 学习路程 -- 查询操作 where

    1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...

  6. LINQ 学习路程 -- 查询操作 GroupBy ToLookUp

    Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...

  7. LINQ 学习路程 -- 查询操作 let into关键字

    IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...

  8. LINQ 学习路程 -- 查询操作 Select, SelectMany

    IList<Student> studentList = new List<Student>() { , StudentName = "John" }, , ...

  9. LINQ 学习路程 -- 查询操作 OfType

    OfType操作根据集合中的元素是否是给定的类型进行筛选 IList mixedList = new ArrayList(); mixedList.Add(); mixedList.Add(" ...

随机推荐

  1. js中删除数组元素的几种方法

    1:js中的splice方法 splice(index,len,[item])    注释:该方法会改变原始数组. splice有3个参数,它也可以用来替换/删除/添加数组内某一个或者几个值 inde ...

  2. PSSM特征-从生成到处理

    以下代码均为个人原创,如有疑问,欢迎交流.新浪微博:拾毅者 本节内容: pssm生成 pssm简化 标准的pssm构建 滑动pssm生成 在基于蛋白质序列的相关预測中.使用PSSM打分矩阵会得将预測效 ...

  3. memcache原理和实际应用

    Memcache是什么 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的.眼下全世界不少人使用这个缓存项目来构建自己大负载的站点,来分担数据库的压力. 它能够应 ...

  4. iOS中三种方式实现登录界面播放视频或gif效果

    现在app都做的越来越炫酷,各种动画效果,各种特效很好的提高了用户的体验.很多app在登录界面都使用了动画效果,比如Uber,Keep,QQ等等.这些动画效果基本都是使用gif或者MP4来实现的. 效 ...

  5. 魔幻特效,慢放世界,nova 3带你玩转抖音新技能

    无论是明暗相交的都市夜色, 还是鲜亮风景前的逆光美人: 无论是瞬息飘飞的叶片, 还是动如脱兔的稚子孩童…… 色彩与速度,精彩的每一刻, 华为摄影都尽在掌握! (华为手机摄影效果) 一直以来,华为的图形 ...

  6. wps文档怎样去除广告

    安装完 WPS 之后右键—属性—打开文件夹位置(如图) 接下来进入 10.1.0.6929 文件夹内,再次进入 office6 文件夹内,找到 wpscenter 应用程序,将其删除.此应用包含定时弹 ...

  7. linux 给用户修改权限

    #添加一个用户 useradd xiaoming #设置密码 passwd xiaoming 回程 //设置密码就行了 #把用户修改成root权限 vi /etc/passwd #找到xiaoming ...

  8. angular选择器功能

    1.$event对象    $event对象其实就是潜在的jQuery事件对象,通过$event.currentTarget获取当前元素,通过$event.target获取当前元素的子元素. 例如: ...

  9. Intellij idea 切换SVN路径

    一直不懂如何切换路径,每次都是删除---->检出:本地源码都不能保存下来,非常麻烦 //在idea中svn切换到新分支:[vcs] -> [subversion] -> [updat ...

  10. Laravel开发:多用户登录验证(1)

    之前实现了一次,后来代码忘记放哪了,所以有跳了一次坑. 先贴上Laravel自带的验证代码: 路由:routes/web.php // Authentication Routes... $this-& ...