语法:
public static TSource Aggregate<TSource>(
this IEnumerable<TSource> source,
Func<TSource, TSource, TSource> func
)

类型参数

TSource

source 中的元素的类型。

参数

source
类型:System.Collections.Generic.IEnumerable<TSource>
要聚合的 IEnumerable<T>
func
类型:System.Func<TSource, TSource, TSource>
要对每个元素调用的累加器函数。

返回值

类型:TSource
累加器的最终值。

 
实例:

private void _do(object param)
{
string sentence = "the quick brown fox jumps over the lazy dog";

// Split the string into individual words.
string[] words = sentence.Split(' ');

string reversed = words.Aggregate(this.funagg);

}

 

/// <summary>
/// Aggregate 中的应用函数
/// </summary>
/// <param name="returns">每次调用这个函数后的返回值,在再次调这个函数时传人</param>
/// <param name="b">集合中的元素</param>
/// <returns></returns>
string funagg(string returns, string b) {
return string.Format("'{1}','{0}'", returns, b);
}

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

类型参数
TSource
        source 中的元素的类型。

TAccumulate
        累加器值的类型。

TResult
       结果值的类型。

参数
source
      类型:System.Collections.Generic.IEnumerable<TSource>
     要聚合的 IEnumerable<T>。
seed
     类型:TAccumulate
     累加器的初始值。
func
    类型:System.Func<TAccumulate, TSource, TAccumulate>
    要对每个元素调用的累加器函数。
resultSelector
    类型:System.Func<TAccumulate, TResult>
    将累加器的最终值转换为结果值的函数。
返回值
   类型:TResult
   已转换的累加器最终值。

举例:

private void _do(object param)
{
  string[] fruits = { "apple", "mango", "orange", "passionfruit", "grape" };
  string longst = fruits.Aggregate("bana", fun1, fun2);
}

/// <summary>
/// 返回两个参数中字符较多那个
/// </summary>
/// <param name="returns">在Aggregate函数循环调用过程中,returns是上一次调用这个函数返回的值,next是集合中的元素 </param>
/// <param name="next"></param>
/// <returns></returns>
string fun1(string returns, string next) {
  return next.Length > returns.Length ? next : returns;
}

/// <summary>
/// 遍历集合(调用函数fun1)结束后,调用这个函数处理fun1返回值
/// </summary>
/// <param name="returns"></param>
/// <returns></returns>
string fun2(string returns) {
  return returns.ToUpper();
}

System.Linq.Enumerable 中的方法 Aggregate 函数的更多相关文章

  1. System.ExecutionEngineException: Attempting to JIT compile method System.Linq.Enumerable

    关于JIT编译和AOT编译的问题.IOS下是不支持JIT动态编译的,所以如果程序有涉及JIT编译的都会无法执行. 在google查过说unity是不支持部分的Linq功能,如Sort方法. 但我在un ...

  2. 整理一下 System.Linq.Enumerable 类中的那些比较少用的方法

    Linq 虽然用得多,但是里面有一些方法比较少用,因此整理一下.Enumerable 类的所有方法可以在 MSDN 上查阅到:https://msdn.microsoft.com/zh-cn/libr ...

  3. 【转载】JS中bind方法与函数柯里化

    原生bind方法 不同于jQuery中的bind方法只是简单的绑定事件函数,原生js中bind()方法略复杂,该方法上在ES5中被引入,大概就是IE9+等现代浏览器都支持了(有关ES5各项特性的支持情 ...

  4. PHP中CURL方法curl_setopt()函数的一些参数

    bool curl_setopt (int ch, string option, mixed value)curl_setopt()函数将为一个CURL会话设置选项.option参数是你想要的设置,v ...

  5. PHP中CURL方法curl_setopt()函数的参数

    PHP CURL curl_setopt 参数 bool curl_setopt (int ch, string option, mixed value)curl_setopt()函数将为一个CURL ...

  6. PHP中CURL方法curl_setopt()函数的一些参数 (转)

    bool curl_setopt (int ch, string option, mixed value) curl_setopt()函数将为一个CURL会话设置选项.option参数是你想要的设置, ...

  7. Go语言中的方法和函数

    在C#或者Java里面我们都知道,一个Class是要包含成员变量和方法的,对于GO语言的Struct也一样,我们也可以给Struct定义一系列方法. 一.怎么定义一个方法? Go的方法是在函数前面加上 ...

  8. print(dir(...)) 打印对象或者类中的方法和函数

  9. .NET中扩展方法和Enumerable(System.Linq)

    LINQ是我最喜欢的功能之一,程序中到处是data.Where(x=x>5).Select(x)等等的代码,她使代码看起来更好,更容易编写,使用起来也超级方便,foreach使循环更加容易,而不 ...

随机推荐

  1. 一次线上http接口调用不通相关的解决过程

    2016-05-25 08:58:34 昨天线上小白系统因为调用外部http接口,超时不释放,导致页面反应很慢,时间一长,报502错误. 上网查了下,502错误是因为服务对于客户的请求没有得到及时的反 ...

  2. Visual Studio 2015 Update 1 ISO

    Visual Studio Community 2015 with UPDATE 1___________________________________________English ENU - h ...

  3. iOS推送生成服务器端p12文件

    生成服务器端推送p12文件 所需文件:A.开发证书  aps_production.cer B.本地导出的私钥   : aps_production.p12 C.生成证书时用到的请求文件:Push.c ...

  4. sis9280触摸ic 基于rk3288 的安卓4.4的 多点触摸

    前言:sis提供的驱动ic.基于rk3288的安卓系统.亲眼看到人家完成一次移植.很激动的记下一些东西..虽然我看不懂.其实现在的工作也不需要看懂.叫人协助就好,只需要知道有这个东西. 1linux下 ...

  5. 在linux使用make编译ArduPilot for Pixhawk/PX4 ArduPilot 编译环境搭建

    Building ArduPilot for Pixhawk/PX4 on Linux with Make 使用Make编译 ArduPilot for Pixhawk 2, Pixhawk and ...

  6. docker push 实现过程

    这一篇文章分析一下docker push的过程:docker push是将本地的镜像上传到registry service的过程: 根据前几篇文章,可以知道客户端的命令是在api/client/pus ...

  7. 1.6jdk + eclipse + pydev搭建Python开发环境

    直接在1.6jdk的eclipse上用install new software的方法安装插件,会找不到安装好的插件.pydev官网还提供一种zip直接解压插件到eclipse文件夹下的dropins文 ...

  8. NOIP 赛前模拟记录

    8.11 T1 给定一个序列M,求出能过构造出的序列使得(Si+Si+1)/2=Mi成立的序列个数.保证M,S递增. T2 平面点集中最大的四边形面积 T3 不太懂得一道国家队选拔的加强版. 90+2 ...

  9. 12、c#中事务及回滚

    public void UpdateContactTableByDataSet(DataSet ds, string strTblName) { try { SqlDataAdapter myAdap ...

  10. 软件工程总结(Final)

    又到了学期末了,此时此刻软件工程课业接近尾声了.时间过得太快,仿佛昨天这学期的课才开始, 开学时,老师让我们提出了不少对这门课的问题和疑惑,通过一学期的努力学习,我可以试着解开我自己的疑问了. 下面是 ...