System.Linq.Enumerable 中的方法 Aggregate 函数
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 函数的更多相关文章
- System.ExecutionEngineException: Attempting to JIT compile method System.Linq.Enumerable
关于JIT编译和AOT编译的问题.IOS下是不支持JIT动态编译的,所以如果程序有涉及JIT编译的都会无法执行. 在google查过说unity是不支持部分的Linq功能,如Sort方法. 但我在un ...
- 整理一下 System.Linq.Enumerable 类中的那些比较少用的方法
Linq 虽然用得多,但是里面有一些方法比较少用,因此整理一下.Enumerable 类的所有方法可以在 MSDN 上查阅到:https://msdn.microsoft.com/zh-cn/libr ...
- 【转载】JS中bind方法与函数柯里化
原生bind方法 不同于jQuery中的bind方法只是简单的绑定事件函数,原生js中bind()方法略复杂,该方法上在ES5中被引入,大概就是IE9+等现代浏览器都支持了(有关ES5各项特性的支持情 ...
- PHP中CURL方法curl_setopt()函数的一些参数
bool curl_setopt (int ch, string option, mixed value)curl_setopt()函数将为一个CURL会话设置选项.option参数是你想要的设置,v ...
- PHP中CURL方法curl_setopt()函数的参数
PHP CURL curl_setopt 参数 bool curl_setopt (int ch, string option, mixed value)curl_setopt()函数将为一个CURL ...
- PHP中CURL方法curl_setopt()函数的一些参数 (转)
bool curl_setopt (int ch, string option, mixed value) curl_setopt()函数将为一个CURL会话设置选项.option参数是你想要的设置, ...
- Go语言中的方法和函数
在C#或者Java里面我们都知道,一个Class是要包含成员变量和方法的,对于GO语言的Struct也一样,我们也可以给Struct定义一系列方法. 一.怎么定义一个方法? Go的方法是在函数前面加上 ...
- print(dir(...)) 打印对象或者类中的方法和函数
- .NET中扩展方法和Enumerable(System.Linq)
LINQ是我最喜欢的功能之一,程序中到处是data.Where(x=x>5).Select(x)等等的代码,她使代码看起来更好,更容易编写,使用起来也超级方便,foreach使循环更加容易,而不 ...
随机推荐
- byte数据的常用操作函数[转发]
/// <summary> /// 本类提供了对byte数据的常用操作函数 /// </summary> public class ByteUtil { ','A','B',' ...
- How to use a 32bit Oracle11_g client in 64 win system and not conflict with sqldeveloper 64 bit tool
At the path:C:\app\USER_NAME\product\11.2.0\client_1\sqldeveloper\sqldeveloper\bin, there a file 'sq ...
- oracle使用sqlplus创建表空间
一.打开命令行窗口,输入以下命令:sqlplus /nolog 回车后,将出现提示符 SQL>, 这时输入conn / as sysdba 一般即可登录,如果失败的话,可以试一下用conn sy ...
- SSH框架
一,Struts2框架部署 1,搭建Struts2的jar包 1.1,Struts2所需各个jar包的作用 asm-3.3.jar 操 ...
- centos6.3安装MySQL 5.6(转)
1.下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 选择“Source Code”, 用已经注册好的oracle账户登录后才能 ...
- SIP模块版本错误问题:the sip module implements API v??? but XXX module requires API v???
系统安装了python 2.7,继续安装PyQt4,于是依次下载sip.pyqt4源码进行安装.用以下代码测试: import PyQt4.QtGui 显示出错.错误信息:the sip module ...
- HDU2544 最短路dij
纯最短路. ///HDU 2544堆优化的最短路 #include <cstdio> #include <iostream> #include <sstream> ...
- 阿里云服务器远程mysql连不上
使用grant all privilege命令添加了远程访问账号,可是还是怎么都连不上,查了好多才知道,原来在linux下,需要开启允许对外访问的网络端口才行. 使用以下步骤即可开启3306端口: / ...
- linux动态时钟探索
在早期的linux内核版本的时间概念都是由周期时钟提供的.虽然比较有效,但是,对于关注能耗电量的系统上,就不能满足长时间休眠的需求,因为周期系统要求必须在一定的频率下,周期性的处于活动状态.因此,li ...
- 传智播客JavaWeb day10-jdbc操作mysql、连接数据库六大步骤
第十天主要讲了jdbc操作mysql数据库,包括连接数据库六大步骤(注册数据库驱动.获得连接对象connetion.生成传输器stament.执行查询获得ResultSet.遍历结果集.关闭资源).介 ...