LINQ中Aggregate的用法
类别:编程学习 浏览量:367
时间:2016-2-23 LINQ中Aggregate的用法
LINQ中Aggregate的用法LINQ中的Aggregate可以做一些复杂的聚合运算,例如累计求和,累计求乘积。
Aggregate接受2个参数,一般第一个参数是称为累积数(默认情况下等于第一个值),而第二个代表了下一个值。第一次计算之后,计算的结果会替换掉第一个参数,继续参与下一次计算。
一、Aggregate的源码分析
Aggregate在内部其实也仅仅是“普通做法”一模一样的源代码,Aggregate做的是一层代码封装
二、LINQ中的Aggregate实例
1、使用Aggregate语法做阶乘运算
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var numbers = GetArray(5);
var result = (from n in numbers
select n).Aggregate(
(total, next) =>
{
return total * next;
});
Console.WriteLine("5的阶乘为:{0}",result);//返回120,也就是1*2*3*4*5
}
static IEnumerable<int> GetArray(int max) {
List<int> result = new List<int>(max);
for (int i = 0; i < max; i++)
{
result.Add(i+1);
}
return result;
}
}
}
2、Aggregate用于集合的简单的累加、阶乘
using System;
using System.Linq;
class Program
{
static void Main()
{
int[] array = { 1, 2, 3, 4, 5 };
int result = array.Aggregate((a, b) => b + a);
// 1 + 2 = 3
// 3 + 3 = 6
// 6 + 4 = 10
// 10 + 5 = 15
Console.WriteLine(result);
result = array.Aggregate((a, b) => b * a);
// 1 * 2 = 2
// 2 * 3 = 6
// 6 * 4 = 24
// 24 * 5 = 120
Console.WriteLine(result);
}
}
//输出结果:
//15
//120
3、Aggregate,在字符串中反转单词的排序
string sentence = "the quick brown fox jumps over the lazy dog";
string[] words = sentence.Split(' ');
string reversed = words.Aggregate((workingSentence, next) =>next + " " + workingSentence);
Console.WriteLine(reversed);
//输出结果:
//dog lazy the over jumps fox brown quick the
4、使用 Aggregate 应用累加器函数和结果选择器
下例使用linq的Aggregate方法找出数组中大于"banana", 长度最长的字符串,并把它转换大写。
string[] fruits ={
"apple", "mango", "orange", "passionfruit", "grape"
};
string longestName =
fruits.Aggregate("banana",
(longest, next) =>
next.Length > longest.Length ? next : longest,
fruit => fruit.ToUpper());
Console.WriteLine(
"The fruit with the longest name is {0}.",longestName);
//输出结果:
//The fruit with the longest name is PASSIONFRUIT.
5、使用 Aggregate 应用累加器函数和使用种子值
下例使用linq的Aggregate方法统计一个数组中偶数的个数。
int[] ints = { 4, 8, 8, 3, 9, 0, 7, 8, 2 };
//统计一个数组中偶数的个数,种子值设置为0,找到偶数就加1
int numEven = ints.Aggregate(0, (total, next) =>
next % 2 == 0 ? total + 1 : total);
Console.WriteLine("The number of even integers is: {0}", numEven);
//输出结果:
//The number of even integers is: 6
标签:LINQ
您可能感兴趣
- 使用 LINQPad 调试linq以及lambda表达式
- linq to sql 中Concat、Union、Intersect、Except
- linq 数据类型转换
- linq distinct去重
- list使用linq排序
- linq中延迟执行
- linq 排序
- Linq中select查询
- 如何查看linq生成的sql
- linq xml 查询
- datatable linq查询
- Linq中的TakeWhile和SkipWhile
- linq中AsEnumerable和AsQueryable的区别
- LINQ TO SQL 中join
- Linq操作Datable
- Linq中where查询
- 萌娃唱《白龙马》走红,那生动的小表情,网友直呼 简直是戏精(萌娃唱白龙马走红)
- 朱鹤松被不断认可,凤凰传奇玲花喊话岳云鹏,索要老朱演出门票(朱鹤松被不断认可)
- 元宵晚会槽点多,芒果台上来就假唱,岳云鹏不说相声改评书了(元宵晚会槽点多)
- 岳云鹏跟凤凰传奇谈心,说出了人生中最重要的三个人,这才成功(岳云鹏跟凤凰传奇谈心)
- 爱情可以当饭吃吗(爱情能当饭吃吗)
- Top 3 JSHS《运动与健康科学 英文 》跻身SCI体育学期刊世界前三(Top3JSHS运动与健康科学)
热门推荐
- linux如何安装php环境(Linux下安装Memcached服务器和客户端与PHP使用示例)
- 虚拟云服务器如何安装宝塔(云服务器宝塔面板的安装图文教程详解)
- 404页面如何设置
- 云服务器/VPS/云主机/服务器修改登陆密码图文教程(云服务器/VPS/云主机/服务器修改登陆密码图文教程)
- sqlserver分表后如何查询(SQL Server中row_number分页查询的用法详解)
- mysql带log的版本(聊聊MYSQL中Redo Log是什么?)
- web安全漏洞怎么解决(Web服务器常见8种安全漏洞)
- pythonjson库(Python常用的json标准库)
- SQLServer将数据导出为SQL脚本
- canvas3d效果(JavaScript canvas实现七彩太阳光晕效果)