博客
关于我
LINQ之Aggregate
阅读量:311 次
发布时间:2019-03-04

本文共 6173 字,大约阅读时间需要 20 分钟。

Aggregate()

可以对数组或列表中的每个元素执行聚合处理。

在以下代码中,通过lambda表达式描述了将数组中所有元素相加而获得的总值的过程。

using System.Linq;using System.Collections;using System.Collections.Generic;public static class Program{       static void Main( string[] args )    {           int[] numbers   = new int[] {    1, 2, 3, 4 };		        int total = numbers.Aggregate( ( sum, next ) => sum += next );        System.Console.WriteLine( "numbers:{0}", numbers.Text() );        System.Console.WriteLine( "total  :{0}", total );        System.Console.ReadKey();    }    public static string Text( this IEnumerable i_source )    {           string text = string.Empty;        foreach( var value in i_source )        {               text += string.Format( "[{0}], ", value );        }        return text;    }} numbers:[1], [2], [3], [4],total :10

Aggregate()令人困惑的部分是如何读取参数指定的过程。让我们看一下。

上面代码Aggregate()中指定( sum, next ) => sum += next的过程。
每个元素都包含在第二个参数中。
在这种情况下,next为1到4不相等的值。
然后,将在处理前一个元素时返回的值加到第一个参数。
尽管它是一个值,但它可以是字符串或类,具体取决于您指定的类型。

sum = 0,next = 1

总和= 1,下一个= 2
总和= 3,下一个= 3
总和= 6,下一个= 4

并且处理完最后一个元素的Aggregate()返回处理后的值。

除了简单地添加之外,您甚至可以按如下所示返回比较值:

using System.Linq;using System.Collections;using System.Collections.Generic;public static class Program{       static void Main( string[] args )    {           int[] numbers   = new int[] {    1, 2, 3, 4 };        int maxNumber   = numbers.Aggregate( ( max, next ) => max < next ? next : max );        System.Console.WriteLine( "numbers:{0}", numbers.Text() );        System.Console.WriteLine( "max    :{0}", maxNumber );        System.Console.ReadKey();    }    public static string Text( this IEnumerable i_source )    {           string text = string.Empty;        foreach( var value in i_source )        {               text += string.Format( "[{0}], ", value );        }        return text;    }} numbers:[1], [2], [3], [4],max :4

在以上过程中,描述了返回最大值的过程。在这种情况下Aggregate()参数的流程如下。

max = 0,下一个= 1

最大= 1,下一个= 2
最大= 2,下一个= 3
最大值= 3,下一个= 4

在上面的代码中,Aggregate()调用第一个元素的第一个参数是每种类型的默认值。我们可以直接指定该值。

using System.Linq;using System.Collections;using System.Collections.Generic;public static class Program{       static void Main( string[] args )    {           int[] numbers   = new int[] {    1, 2, 3, 4 };        int   seed      = 15;        int total = numbers.Aggregate( seed, ( sum, next ) => sum += next );        System.Console.WriteLine( "numbers:{0}", numbers.Text() );        System.Console.WriteLine( "seed   :{0}", seed );        System.Console.WriteLine( "total  :{0}", total );        System.Console.ReadKey();    }    public static string Text( this IEnumerable i_source )    {           string text = string.Empty;        foreach( var value in i_source )        {               text += string.Format( "[{0}], ", value );        }        return text;    }}numbers:[1], [2], [3], [4],seed :15total :25

在此示例中,我们传递15作为初始值。下面是Aggregate()的流程。

sum = 15, next = 1

sum = 16, next = 2
sum = 18, next = 3
sum = 21, next = 4

该初始值(seed)可以是与调用序列不同的类型。

在这种情况下Aggregate(),返回的类型将与种子值相同。

using System.Linq;using System.Collections;using System.Collections.Generic;public static class Program{       static void Main( string[] args )    {           int[]   numbers   = new int[] {    1, 2, 3, 4 };        string  seed      = "0";        string numberText   = numbers.Aggregate( seed, ( text, next ) => string.Format( "{0}+{1}", text, next.ToString() ) );        System.Console.WriteLine( "numbers:{0}", numbers.Text() );        System.Console.WriteLine( "seed   :{0}", seed );        System.Console.WriteLine( "text   :{0}", numberText );        System.Console.ReadKey();    }    public static string Text( this IEnumerable i_source )    {           string text = string.Empty;        foreach( var value in i_source )        {               text += string.Format( "[{0}], ", value );        }        return text;    }} numbers:[1], [2], [3], [4],seed :0text :0+1+2+3+4

在上面的示例中,我们将字符串seed传递给int数组。

因此,Aggregate()如果您查看参数流程,它看起来像这样。

text = “0”, next = 1

text = “0+1”, next = 2
text = “0+1+2”, next = 3
text = “0+1+2+3”, next = 4

前面都是对最后一个元素执行处理后的返回值用作返回值。

我们也可以在第三个参数中对最终结果进行处理。

using System.Linq;using System.Collections;using System.Collections.Generic;public static class Program{       static void Main( string[] args )    {           int[]   numbers     = new int[] {    1, 2, 3, 4 };        int     seed        = 15;        int totalSquare     = numbers.Aggregate(                                         seed,                                        ( sum, next ) => sum += next,                                        ( sum ) => sum * sum );        System.Console.WriteLine( "numbers    :{0}", numbers.Text() );        System.Console.WriteLine( "seed       :{0}", seed );        System.Console.WriteLine( "totalSquare:{0}", totalSquare );        System.Console.ReadKey();    }    public static string Text( this IEnumerable i_source )    {           string text = string.Empty;        foreach( var value in i_source )        {               text += string.Format( "[{0}], ", value );        }        return text;    }}numbers :[1], [2], [3], [4],seed :15totalSquare:625

在上面的示例中,流程如下。

seed = 15;
sum = 10 + seed;
sum = 25;
sum*sum = 625;

using System.Linq;using System.Collections;using System.Collections.Generic;public static class Program{       static void Main( string[] args )    {           int[]   numbers = new int[] {    1, 2, 3, 4 };        string  seed    = "";        int number      = numbers.Aggregate(                                         seed,                                        ( text, next ) => string.Format( "{0}{1}", text, next.ToString() ),                                        ( text ) => int.Parse( text ) );        System.Console.WriteLine( "numbers:{0}", numbers.Text() );        System.Console.WriteLine( "seed   :{0}", seed );        System.Console.WriteLine( "number :{0}", number );        System.Console.ReadKey();    }    public static string Text( this IEnumerable i_source )    {           string text = string.Empty;        foreach( var value in i_source )        {               text += string.Format( "[{0}], ", value );        }        return text;    }} numbers:[1], [2], [3], [4],seed :number :1234

在此示例中,描述了将每个元素的数值添加为字符串并最终将字符串转换为数值的过程。

转载地址:http://idnq.baihongyu.com/

你可能感兴趣的文章
NIFI大数据进阶_Kafka使用相关说明_实际操作Kafka消费者处理器_来消费kafka数据---大数据之Nifi工作笔记0037
查看>>
NIFI大数据进阶_Kafka使用相关说明_实际操作Kafka生产者---大数据之Nifi工作笔记0036
查看>>
NIFI大数据进阶_NIFI的模板和组的使用-介绍和实际操作_创建组_嵌套组_模板创建下载_导入---大数据之Nifi工作笔记0022
查看>>
NIFI大数据进阶_NIFI监控功能实际操作_Summary查看系统和处理器运行情况_viewDataProvenance查看_---大数据之Nifi工作笔记0026
查看>>
NIFI大数据进阶_NIFI监控的强大功能介绍_处理器面板_进程组面板_summary监控_data_provenance事件源---大数据之Nifi工作笔记0025
查看>>
NIFI大数据进阶_NIFI集群知识点_认识NIFI集群以及集群的组成部分---大数据之Nifi工作笔记0014
查看>>
NIFI大数据进阶_NIFI集群知识点_集群的断开_重连_退役_卸载_总结---大数据之Nifi工作笔记0018
查看>>
NIFI大数据进阶_内嵌ZK模式集群1_搭建过程说明---大数据之Nifi工作笔记0015
查看>>
NIFI大数据进阶_外部ZK模式集群1_实际操作搭建NIFI外部ZK模式集群---大数据之Nifi工作笔记0017
查看>>
NIFI大数据进阶_实时同步MySql的数据到Hive中去_可增量同步_实时监控MySql数据库变化_操作方法说明_01---大数据之Nifi工作笔记0033
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_01_实际操作---大数据之Nifi工作笔记0029
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_02_实际操作_splitjson处理器_puthdfs处理器_querydatabasetable处理器---大数据之Nifi工作笔记0030
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_说明操作步骤---大数据之Nifi工作笔记0028
查看>>
NIFI大数据进阶_连接与关系_设置数据流负载均衡_设置背压_设置展现弯曲_介绍以及实际操作---大数据之Nifi工作笔记0027
查看>>
NIFI数据库同步_多表_特定表同时同步_实际操作_MySqlToMysql_可推广到其他数据库_Postgresql_Hbase_SqlServer等----大数据之Nifi工作笔记0053
查看>>
NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南001---大数据之Nifi工作笔记0068
查看>>
NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南002---大数据之Nifi工作笔记0069
查看>>
NIFI集群_内存溢出_CPU占用100%修复_GC overhead limit exceeded_NIFI: out of memory error ---大数据之Nifi工作笔记0017
查看>>
NIFI集群_队列Queue中数据无法清空_清除队列数据报错_无法删除queue_解决_集群中机器交替重启删除---大数据之Nifi工作笔记0061
查看>>
NIH发布包含10600张CT图像数据库 为AI算法测试铺路
查看>>