c# Enumerable中Aggregate和Join的使用
参考页面:
http://www.yuanjiaocheng.net/ASPNET-CORE/asp.net-core-environment.html
http://www.yuanjiaocheng.net/ASPNET-CORE/newproject.html
http://www.yuanjiaocheng.net/webapi/web-api-gaisu.html
http://www.yuanjiaocheng.net/webapi/create-web-api-proj.html
http://www.yuanjiaocheng.net/webapi/test-webapi.html
直接上代码:
IEnumerable<, ); int all = list.Aggregate((sum, index) => sum + index);
调试, 第一次调用,发现sum和index分别取列表的第1和第2个值:

F5下一步,发现把index加到sum了 (sum += index), 然后index取下一个值, 并累积到sum,重复此步骤直到取完列表中的值:

最后计算结果是65
另外2个重载函数:
, (sum, index) => sum + index);
第2个参数与上一个例子参数一样,累积列表中值,第1个参数是初始值, 会应用到累积值,在这里相当于用10加65,计算结果75。
, (sum, index) => sum + index, res => res == );
第1第2个参数同上,第3个参数是对累积结果做判断,在这个例子里判断累积结果是否等于75,计算结果是true。
从中可以发现,list.Aggregate((sum, index) => sum + index)其实是list.Aggregate(0, (sum, index) => sum + index)的特例,相当于初始值为0而已。
===================
Join使用,看代码
Persion结构:
public struct Persion
{
public int index;
public string name;
public Persion(int index, string nm)
{
this.index = index;
name = nm;
}
}
Pet结构:
public struct Pet
{
public string name;
public Persion owner;
public Pet(string name, Persion person)
{
this.name = name;
owner = person;
}
}
Persion_Pet结构:
public struct Persion_Pet
{
public string persionName;
public string petName;
public Persion_Pet(string persion, string pet)
{
persionName = persion;
petName = pet;
}
}
Join使用:
Persion p1 = , "张三");
Persion p2 = , "李四");
Persion p3 = , "路人甲");
List<Persion> people = new List<Persion>() { p1, p2, p3 };
Persion p4 = , "路人乙");
Pet dog = new Pet("欢欢", p1);
Pet cat = new Pet("咪咪", p2);
Pet cat2 = new Pet("cat2", p4);
List<Pet> petList = new List<Pet>() { dog, cat, cat2 };
var res = people.Join(petList, persion => persion, pet => pet.owner, (persion, pet) => new Persion_Pet(persion.name, pet.name)).ToList();
结果:

关键在于join会比较第2个参数与第3个参数的返回值,只有相等时才会继续第4个参数。
c# Enumerable中Aggregate和Join的使用的更多相关文章
- JAVA中的Fork/Join框架
看了下Java Tutorials中的fork/join章节,整理下. 什么是fork/join框架 fork/join框架是ExecutorService接口的一个实现,可以帮助开发人员充分利用多核 ...
- hadoop中MapReduce多种join实现实例分析
转载自:http://zengzhaozheng.blog.51cto.com/8219051/1392961 1.在Reudce端进行连接. 在Reudce端进行连接是MapReduce框架进行表之 ...
- Linq 中的 left join
Linq 中的 left join 表A User: 表B UserType: Linq: from t in UserType join u in User on t.typeId equal u. ...
- Linq To Sql中实现Left Join与Inner Join使用Linq语法与lambda表达式
当前有两个表,sgroup与sgroupuser,两者通过gKey关联,而sgroup表记录的是组,而sgroupuser记录是组中的用户,因此在sgroupuser中不一定有数据.需要使用Left ...
- T-SQL 中的CROSS JOIN用法(半翻译)
突然发现个很吊的链接,我们来看看学习数据库要做些什么,胆小慎点:DBA工作内容!!!! 今天来翻译一篇关于T-SQL的文章,本文可供微软认证70-461:QueryingMicrosoft SQL S ...
- Oracle中 (+)与left join 的用法区别
Oracle中 (+)与left join 的用法区别 原创 2017年01月11日 13:33:42 6648 select * from a,b where a.id=b.id(+); (+)写在 ...
- Mysql 中Left/Right join on后面and和where条件查询的差异-Mysql SQL运算符是有优先级
一.Mysql中Left/Right join on后面and和where条件查询的差异 1.建两张测试表,一张商户定义表.一张商户操作状态明细表 1)商户定义表 CREATE TABLE hope. ...
- python3与python2中的string.join()函数
在python2中,string 模块中有一个join()函数,用于以特定的分隔符分隔源变量中的字符串,将其作为新的元素加入到一个列表中,例如: body=string.join(( "Fr ...
- MySQL中使用INNER JOIN来实现Intersect并集操作
MySQL中使用INNER JOIN来实现Intersect并集操作 一.业务背景 我们有张表设计例如以下: CREATE TABLE `user_defined_value` ( `RESOURCE ...
随机推荐
- Travis CI用来持续集成你的项目
这里持续集成基于GitHub搭建的博客为项目 工具: zqz@ubuntu:~$ node --version v4.2.6 zqz@ubuntu:~$ git --version git versi ...
- WebForm获取GET或者POST参数到实体的转换,ADO.NET数据集自动转换实体
最近在修改维护以前的webform项目(维护别人开发的.....)整个aspx没有用到任何的控件,这个我也比较喜欢不用控件所以在提交信息的时候需要自己手动的去Request.QueryString[] ...
- UWP开发之Template10实践二:拍照功能你合理使用了吗?(TempState临时目录问题)
最近在忙Asp.Net MVC开发一直没空更新UWP这块,不过有时间的话还是需要将自己的经验和大家分享下,以求共同进步. 在上章[UWP开发之Template10实践:本地文件与照相机文件操作的MVV ...
- C#调用C++代码遇到的问题总结
最近在开发服务后台的时候,使用c#调用了多个c++编写的dll,期间遇到了一系列的问题,经过一番努力最后都一一解决了,在此做个总结,方便以后参考,毕竟这些问题也都是很常见的,主要有以下问题: 类型对照 ...
- ASP.NET MVC学习之母版页和自定义控件的使用
一.母板页_Layout.cshtml类似于传统WebForm中的.master文件,起到页面整体框架重用的目地1.母板页代码预览 <!DOCTYPE html> <html> ...
- 修改session垃圾回收几率
<?php //修改session垃圾回收几率 ini_set('session.gc_probability','1'); ini_set('session.gc_divisor','2'); ...
- linux拷贝命令,移动命令
http://blog.sina.com.cn/s/blog_7479f7990101089d.html
- jquery-treegrid树状表格的使用(.Net平台)
上一篇介绍了DataTable,这一篇在DT的基础之上再使用jquery的一款插件:treegrid,官网地址:http://maxazan.github.io/jquery-treegrid/ 一. ...
- iOS在导航栏上居中显示分段控件(UISegmentedControl)
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:nil]; segmentedCont ...
- 递归实现n(经典的8皇后问题)皇后的问题
问题描述:八皇后问题是一个以国际象棋为背景的问题:如何能够在8×8的国际象棋棋盘上放置八个皇后, 使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行.纵行或斜线上 ...