语法:
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. Android WebView useragent

    今天介绍一下Android WebView UserAgent, User-Agent(简称UA)是HTTP请求头部用来标识客户端信息的字符串, 包括操作系统, 浏览器等信息.为了建立手机客户端的信息 ...

  2. swift 定位

    iOS 8 及以上需要在info.plist文件中添加下面两个属性 NSLocationWhenInUseUsageDescription 使用应用期间 NSLocationAlwaysUsageDe ...

  3. 计算机开放电子书汇总(包括二十多本python相关的图书教程)

    计算机开放电子书汇总(包括二十多本python相关的图书教程) https://github.com/it-ebooks/it-ebooks-archive 这个汇总包含了各种计算机相关的开放图书和文 ...

  4. 第九章 硬件抽象层:HAL

    这一章介绍HAL,全称为Hardware Abstract Layer,即硬件抽象层,它是建立在Linux驱动之上的一套程序库,程序库并不属于Linux内核,而是属于Linux内核层之上的应用层.为A ...

  5. Fragment的使用(二)

    动态添加Fragment 将MainActivity的布局文件中的两个Fragment的代码注释掉.在MainActivity的java文件中添加如下代码: package com.cm.myfrag ...

  6. Unity中游戏的声音管理

    using UnityEngine;using System.Collections;using System.Collections.Generic;/// <summary>/// 用 ...

  7. 禁用 baloo_file_extractor 加速 ubuntu 14.04 (KDE)

    在复制了一堆零散文件后,系统同然变得奇卡,看看cpu和ram都占用不高,但看到这个进程 baloo_file_extractor 时不时地冒一下泡,怀疑是它在频繁访问硬盘.禁止它自动启动的方式: $ ...

  8. No.2 CAS之SPNEGO+LDAP认证配置

    1.概述 本文先配置了SPNEGO认证,就是如果用户操作系统如果登陆了公司的Windows域,用户浏览器访问应用服务即可免登录. 然后如果不在域里的员工,用LDAP认证方式,输账号密码登陆. 参考文档 ...

  9. oracle xmltype导入并解析Excel数据 (三)解析Excel数据

    包声明 create or replace package PKG_EXCEL_UTILS is -- Author: zkongbai-- Create at: 2016-07-06-- Actio ...

  10. 库函数API和C语言汇编语言混合式编程

    C语言代码内嵌汇编的方法: 在C语言文件中以如下格式加入汇编代码 __asm__( “汇编语句模板” :输出部分 :输入部分 :“破坏描述部分” ) asm可以由__asm__代替,为其别名. 可加上 ...