Aggregate这个语法可以做一些复杂的聚合运算,例如累计求和,累计求乘积。它接受2个参数,一般第一个参数是称为累积数(默认情况下等于第一个值),而第二个代表了下一个值。第一次计算之后,计算的结果会替换掉第一个参数,继续参与下一次计算。

一、Aggregate用于集合的简单的累加、阶乘

.using System;
.using System.Linq;
.
.class Program
.{
.static void Main()
.{
.int[] array = { , , , , };
.int result = array.Aggregate((a, b) => b + a);
.// 1 + 2 = 3
.// 3 + 3 = 6
.// 6 + 4 = 10
.// 10 + 5 = 15
.Console.WriteLine(result);
.
. result = array.Aggregate((a, b) => b * a);
.// 1 * 2 = 2
.// 2 * 3 = 6
.// 6 * 4 = 24
.// 24 * 5 = 120
.Console.WriteLine(result);
.}
.}

输出结果:

15
120
Aggregate它接受2个参数,一般第一个参数是称为累积数(默认情况下等于第一个值),而第二个代表了下一个值。

二、Aggregate,在字符串中反转单词的排序

.string sentence = "the quick brown fox jumps over the lazy dog";
.string[] words = sentence.Split(' ');
.string reversed = words.Aggregate((workingSentence, next) =>
. next + " " + workingSentence);
.Console.WriteLine(reversed);
输出结果:
dog lazy the over jumps fox brown quick the 

三、使用 Aggregate 应用累加器函数和结果选择器

下例使用linq的Aggregate方法找出数组中大于"banana", 长度最长的字符串,并把它转换大写。
.string[] fruits = { "apple", "mango", "orange", "passionfruit", "grape" };
.string longestName =
. fruits.Aggregate("banana",
.(longest, next) =>
. next.Length > longest.Length ? next : longest,
. fruit => fruit.ToUpper());
.Console.WriteLine(
."The fruit with the longest name is {0}.",
. longestName);
输出结果:
The fruit with the longest name is PASSIONFRUIT. 

四、使用 Aggregate 应用累加器函数和使用种子值

下例使用linq的Aggregate方法统计一个数组中偶数的个数。
.int[] ints = { , , , , , , , ,  };
.
.//统计一个数组中偶数的个数,种子值设置为0,找到偶数就加1
.int numEven = ints.Aggregate(, (total, next) =>
. next % == ? total + : total);
.
.Console.WriteLine("The number of even integers is: {0}", numEven);

输出结果:

The number of even integers is: 6

除此之外LINQ中的Aggregate还可以用于递归调用。

LINQ中的Aggregate用法总结的更多相关文章

  1. Linq 中 Join 的用法

    Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下. 注:本文内容主要来自<Linq实战>,本例中用到的对象请见文章底部. 1. 组连接 组连接是与分组查询是一样的. ...

  2. linq中join的用法

    join方法 public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>( this IEnu ...

  3. Linq中常用语法

    using System;using System.Collections.Generic;using System.ComponentModel.Design;using System.Linq;u ...

  4. Linq中关键字的作用及用法

    Linq中关键字的作用及用法 1.All:确定序列中的所有元素是否都满足条件.如果源序列中的每个元素都通过指定谓词中的测试,或者序列为空,则为 true:否则为 false. Demo: 此示例使用 ...

  5. Linq中join & group join & left join 的用法

    Linq中join & group join & left join 的用法 2013-01-30 11:12 12154人阅读 评论(0) 收藏 举报  分类: C#(14)  文章 ...

  6. 简述Linq中.ToList(), .AsEnumerable(), AsQueryable()的区别和用法

    [TOC] 这3个方法的功能完全不同, 应按照具体业务场景使用. AsQueryable() 先说说什么是 IQueryable IQueryable 是当前的 data provider 返回的类型 ...

  7. Linq中 AsQueryable(), AsEnumerable()和ToList()的区别和用法

    Linq中 AsQueryable(), AsEnumerable()和ToList()的区别和用法:在写LINQ语句的时候,往往会看到AsEnumerable() ,AsQueryable() 和T ...

  8. MVC+Spring.NET+NHibernate .NET SSH框架整合 C# 委托异步 和 async /await 两种实现的异步 如何消除点击按钮时周围出现的白线? Linq中 AsQueryable(), AsEnumerable()和ToList()的区别和用法

    MVC+Spring.NET+NHibernate .NET SSH框架整合   在JAVA中,SSH框架可谓是无人不晓,就和.NET中的MVC框架一样普及.作为一个初学者,可以感受到.NET出了MV ...

  9. LINQ中的连接(join)用法示例

    Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下. 1. 组连接 组连接是与分组查询是一样的.即根据分组得到结果. 如下例,根据publisther分组得到结果. 使用组连接的 ...

随机推荐

  1. 畅通工程续(dijskra+SPFA)

    畅通工程续 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submiss ...

  2. hdu 2054 A == B ? (java)

    问题: 考虑问题不周到.没有考虑到可能是小数并且存在 1.0=1.01=1的情况. 本题使用了BigDecimal类,此类适用于高精度的数此时攻克了小数和01=1的问题, 该类比較方式中n.equal ...

  3. The Tips of Success(成功的建议)

    1.Do one thing at a time,and do well. 2.Never forget to say "thanks". 3,Keep on going.Neve ...

  4. EF5 通用数据层 增删改查操作,泛型类(转)

    using System; using System.Collections.Generic; using System.Data.Entity.Infrastructure; using Syste ...

  5. 使用prototype扩展的JavaScript常用函数库

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...

  6. C语言栈的实现

    栈是常用的数据结构之一,下面给出一个链式栈的实现~~头文件Stack.h #ifndef Stack_H #define Stack_H typedef int Item; typedef struc ...

  7. iOS对项目中第三方类库的管理——CocoaPods

    http://blog.csdn.net/lengshengren/article/details/1767 唐巧的博客:http://www.devtang.com/blog/2014/05/25/ ...

  8. nginx负载均衡配置(转)

    www.s135.com 和 blog.s135.com 域名均指向 Nginx 所在的服务器IP. 用户访问http://www.s135.com,将其负载均衡到192.168.1.2:80.192 ...

  9. linux杂记(三)linux指令介绍

    [root@linux ~]# command [-options] parameter1 parameter2 说明: 最左边的root显示的是[目前使用者的账号],而@之后接的是linux即[主机 ...

  10. codeforces 13E . Holes 分块

    题目链接 nextt数组表示这个位置的下一个位置. cnt数组表示这个位置 i 到nextt[i]可以弹几次. end[i] 表示在从 i 弹出去的情况下, 最后一个位置是哪里. 然后就看代码吧. # ...