Aggregate累加器
今天看东西的时候看见这么个扩展方法Aggregate(累加器)很是陌生,于是乎查了查,随手记录一下。
直接看一个最简答的版本,其他版本基本没什么区别,需要的时候可看一下
public static TSource Aggregate<TSource>(
this IEnumerable<TSource> source,
Func<TSource, TSource, TSource> func
)
这个方法的功能其实是对,可枚举的IEnumerable<TSource>某种数据,从前两个遍历对象开始逐个,作为输入进行自定
义的操作,这个方法还是蛮有用的,看几个例子。
private void button7_Click(object sender, EventArgs e)
{
string sentence = "the quick brown fox jumps over the lazy dog";
string[] words = sentence.Split(' ');
Func<string, string, string> temp = test;
string reversed = words.Aggregate("",temp);
//string reversed=words.Aggregate((workingSentence, next) =>next + " " + workingSentence);
MessageBox.Show(reversed);
} public string test(string para1, string para2)
{
return para2 + " " + para1;
}
这里我没用msdn直接提供的lambda方式,目的就是为了方便调试查看下。先给出结果吧dog lazy the over jumps fox brown quick the
其执行过程是这样滴,第一次 para1 为the ,para2为 quick,返回了 quick the, 并且作为下次的 para1,para2 为brown ,如此依次的遍历执行下去,直至结束。
再给一个例子可以看出许多应用。计算数据中的整数的个数
private void button8_Click(object sender, EventArgs e)
{
int[] ints = { 4, 8, 8, 3, 9, 0, 7, 8, 2 };
int numEven = ints.Aggregate(0, (total, next) =>next % 2 == 0 ? total + 1 : total);
MessageBox.Show("The number of even integers is: " + numEven);
}
恩恩,这都是msdn直接给出的例子,那么很多类似的统计或者计算累的需求是不是就可以考虑下Aggregate了。
Aggregate累加器的更多相关文章
- Linq: Aggregate
Aggregate累加器 今天看东西的时候看见这么个扩展方法Aggregate(累加器)很是陌生,于是乎查了查,随手记录一下. 直接看一个最简答的版本,其他版本基本没什么区别,需要的时候可看一下 pu ...
- MVC中IActionFilter过滤器俄罗斯套娃的实现方式
看mvc的源码我们知道,它是在 ControllerActionInvoker 类中执行 InvokeAction 方法来实现过滤器和action方法执行的. 通过查看源码我们知道,他是通过调用 In ...
- Linq扩展方法之Aggregate 对序列应用累加器函数
Linq扩展方法之Aggregate 对序列应用累加器函数; 函数模板:// 函数名:对序列应用累加器函数. // Parameters:参数要求 // source:要聚合的 System.Col ...
- C#累加器函数Aggregate用法 讲解
Enumerable.Aggregate 扩展方法在System.Linq命名空间中,是Enumerable类的第一个方法(按字母顺序排名),但确是Enumerable里面相对复杂的方法. MSDN对 ...
- System.Linq.Enumerable 中的方法 Aggregate 函数
语法: public static TSource Aggregate<TSource>( this IEnumerable<TSource> source, Func&l ...
- Linq查询操作之聚合操作(count,max,min,sum,average,aggregate,longcount)
在Linq中有一些这样的操作,根据集合计算某一单一值,比如集合的最大值,最小值,平均值等等.Linq中包含7种操作,这7种操作被称作聚合操作. 1.Count操作,计算序列中元素的个数,或者计算满足一 ...
- Aggregate
对序列应用累加器函数. /// <summary> /// 计算校验和,SUM /// </summary> public byte CalculateCheckSum(byt ...
- IEnumerable接口的Aggregate方法
以前小猪为了累加一个集合中的类容通常会写出类似这样的C#代码: string result ="": foreach (var item in items) { result+=i ...
- LINQ中的Aggregate用法总结
Aggregate这个语法可以做一些复杂的聚合运算,例如累计求和,累计求乘积.它接受2个参数,一般第一个参数是称为累积数(默认情况下等于第一个值),而第二个代表了下一个值.第一次计算之后,计算的结果会 ...
随机推荐
- Django and Djangorestframework
NOte Today, another day debuging with my teammates, and I just tried to make complete comprehension ...
- KoaHub平台基于Node.js开发的Koa的模板系统handlebars插件代码详情
koahub-handlebars koahub-handlebars koahub handlebars templates Installation $ npm install koahub-ha ...
- Gridview AutoGenerateColumns属性
第一篇随笔,以后会陆续的把刚开始工作时的知识点都记录下来,毕竟现在用WebForm的不多了~ AutoGenerateColumns MSDN 说明 : 获取或设置一个值,该值指示是否为数据源中的每个 ...
- ubuntu auto mount自动挂载硬盘
Ubuntu 挂载的文章在网上也不少,推荐一个: http://wenku.baidu.com/link?url=N2c7axijp_KYaYkt2CrZFNZPzzS8xBHLQSTUcI2F85I ...
- mysql语句sum求和为null的问题
select sum(price) as price from order where status='SUCCESS'; 如果price对应的所有的值为0,那么算出来的和为null: 可以采用ifn ...
- PHP生成随机水印图片
基于PHP的GD图形库,自己生成一张图片.仅限初识GD库,实例学习. 一.需求 网站的布局用到了类似慕课网课程列表的风格,每一个课程是一个banner图,图下面是标题加简介.因为课程的数量较大没有为所 ...
- cuda编程学习3——VectorSum
这个程序是把两个向量相加 add<<<N,1>>>(dev_a,dev_b,dev_c);//<N,1>,第一个参数N代表block的数量,第二个参数1 ...
- 5种方法推导Normal Equation
引言: Normal Equation 是最基础的最小二乘方法.在Andrew Ng的课程中给出了矩阵推到形式,本文将重点提供几种推导方式以便于全方位帮助Machine Learning用户学习. N ...
- (转)Nginx启动出错 error while loading shared libraries
[root@localhost conf]# /usr/local/nginx/sbin/nginx/usr/local/nginx/sbin/nginx: error while loading s ...
- Hadoop2.7.3分布式集群安装
一.依赖文件安装 1.1 JDK 参见博文:http://www.cnblogs.com/liugh/p/6623530.html 二.文件准备 2.1 文件名称 hadoop-2.7.3.tar.g ...