Linq扩展方法之Aggregate 对序列应用累加器函数
Linq扩展方法之Aggregate 对序列应用累加器函数;
函数模板:// 函数名:对序列应用累加器函数。
// Parameters:参数要求
// source:要聚合的 System.Collections.Generic.IEnumerable`1。
// func:要对每个元素调用的累加器函数。
// Type parameters:参数类型
// TSource:source 中的元素的类型。
//
// Returns:累加器的最终值。
// Exceptions:
// T:System.ArgumentNullException:
// source 或 func 为 null。
// T:System.InvalidOperationException:
// source 中不包含任何元素。
public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func);
int[] numbers1 = { , , , , , , , , };
var query1 = numbers1.Aggregate((a, b) => a * b);
a的依次变更为:
a=1,a*b=1*2;
a=2,a*b=2*3;
a=6,a*b=6*4;
a=24,a*b=24*5;
a=120,a*b=120*6=720;................
// 对序列应用累加器函数。 将指定的种子值用作累加器初始值。
public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func);
int[] numbers = { , , , , , , , , }; var query = numbers.Aggregate(, (a, b) => ((a < b) ? (a * b) : a));
a的依次变更为:
a=5,((a < b) ? (a * b) : a)):5*9=45;
第二次时:a值为45,b 为3;
第三次此时:a值为45,b 为3;
。。。。。。。。
自定义方法实现
// Summary:
// 对序列应用累加器函数。 将指定的种子值用作累加器的初始值,并使用指定的函数选择结果值。
//
// Parameters:
// source:
// 要聚合的 System.Collections.Generic.IEnumerable`1。
//
// seed:
// 累加器的初始值。
//
// func:
// 要对每个元素调用的累加器函数。
//
// resultSelector:
// 将累加器的最终值转换为结果值的函数。
//
// Type parameters:
// TSource:
// source 中的元素的类型。
//
// TAccumulate:
// 累加器值的类型。
//
// TResult:
// 结果值的类型。
//
// Returns:
// 已转换的累加器最终值。
//
// Exceptions:
// T:System.ArgumentNullException:
// source 或 func 或 resultSelector 为 null。
public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector);
Linq扩展方法之Aggregate 对序列应用累加器函数的更多相关文章
- 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法
比如Json.NET的JObject明明实现了IEnumerable<T>,具体来说是IEnumerable<KeyValuePair<string, JToken>&g ...
- 用LinQ扩展方法,泛型扩展方法,实现自定义验证字符是否空、对象是否为null,及泛型约束使用,Action的使用
一.Linq扩展方法 1.扩展方法必须是静态方法.扩展方法所在的类必须是静态类 2.扩展方法里面的参数必须制定this关键字,紧跟需要扩展的类型,如下: 二.泛型约束 1.使用泛型的原因,是在不知道需 ...
- ABP框架源码中的Linq扩展方法
文件目录:aspnetboilerplate-dev\aspnetboilerplate-dev\src\Abp\Collections\Extensions\EnumerableExtensions ...
- 【手记】走近科学之为什么JObject不能调用LINQ扩展方法
Json.NET的JObject明明实现了IEnumerable<T>,具体来说是IEnumerable<KeyValuePair<string, JToken>> ...
- Linq扩展方法获取单个元素
在使用Linq 提供的扩展方法时,First(OrDefault), Single(OrDefault), Last(OrDefault)都具有返回单个元素的功能.MSDN对这些方法的描述只有功能说明 ...
- LinQ—扩展方法
概述 本节主要解说扩展方法,涉及LinQ的详细知识不多. 扩展方法的描写叙述 .net framework为编程人员提供了非常多的类,非常多的方法,可是,不论.net framework在类中为我们提 ...
- Linq扩展方法之All 、Any
// Summary: // 确定序列中的所有元素是否满足条件. // Parameters: // source:包含要应用谓词的元素的 System.Collections.Generic.IEn ...
- Stackoverflow 珠玑:用于分组的 LINQ 扩展方法
从 stackoverflow.com 上抄来的,将 IEnumerable 中的元素进行切分的方法,无动态内存分配,地球上最快的实现: public static class LinqExtensi ...
- C#编程(六十一)------------LINQ中的扩展方法
原文链接: http://blog.csdn.net/shanyongxu/article/details/47208401 LINQ中的扩展方法 LINQ中where扩展方法,要想使用,必须导入us ...
随机推荐
- windows下安装php真正的多线程扩展pthreads教程
扩展地址:http://docs.php.net/manual/zh/book.pthreads.php 注意事项php5.3或以上,且为线程安全版本.apache和php使用的编译器必须一致.通过p ...
- 启动redis出现Creating Server TCP listening socket *:6379: bind: No such file or directory
E:\redis>redis-server.exe redis.windows.conf [8564] 10 Oct 20:00:36.745 # Creating Server TCP lis ...
- 17.1.2.1 Advantages and Disadvantages of Statement-Based and Row-Based Replication 基于语句和行的复制的优势和劣势
17.1.2.1 Advantages and Disadvantages of Statement-Based and Row-Based Replication 基于语句和行的复制的优势和劣势 每 ...
- ActivityManager的使用
本节内容主要是讲解ActivityManager的使用,通过ActivityManager我们可以获得系统里正在运行的activities,包括 进程(Process)等.应用程序/包.服务(Serv ...
- System.Windows.Forms中的Message Structure
结构用途说明Implements a Windows message. Properties 1.public IntPtr HWnd { get; set; } Gets or sets the w ...
- 产品设计中先熟练使用铅笔 不要依赖Axure
在互联网产品领域,Axure已成为产品经理.产品设计师以及交互设计师的必备工具,从某种程度讲,Axure帮助我们建立低保真模型,便于与用户的需求验证,也帮助我们构思交互细节,使前端和开发人员更容易理解 ...
- bzoj1597
首先不难想到排序,这种无规律的东西一般都要转化为有规律才好做 首先以x为第一关键字,y为第二关键字升序排序 拍完序我们发现,若存在两块土地i,j x[i]<=x[j],y[i]<=y[j] ...
- 动态规划(DP计数):HDU 5121 Just A Mistake
As we all know, Matt is an outstanding contestant in ACM-ICPC. Graph problems are his favorite.Once, ...
- C# partial 局部类型
关键字partial是一个上下文关键字,只有和 class.struct.interface 放在一起时才有关键字的含义.因此partial的引入不会影响现有代码中名称为partial的变量.局部类型 ...
- 关于.NET三层 分类: C#
三层体系结构的概念 用户界面表示层(USL) 业务逻辑层(BLL) 数据访问层(DAL) BLL将USL与DAL隔开了,并且加入了业务规则 各层的作用 1:数据数据访问层:主要是对原始数据(数据库或者 ...