LINQ聚合算法解释

编程入门 行业动态 更新时间:2024-10-12 08:24:45
本文介绍了LINQ聚合算法解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这可能听起来有点扯,但我一直没能找到一个很好的解释总结。

This might sound lame, but I have not been able to find a really good explanation of Aggregate.

好办法简短的描述,COM prehensive有一个小而明显的例子。

Good means short, descriptive, comprehensive with a small and clear example.

推荐答案

最容易理解的总结的定义是,它在列表采取的每一个元素上执行的操作考虑到之前已经在操作。也就是说它执行第一和第二元件上的动作并执行的结果前进。然后,它运行于previous的结果,第三个元素,并发扬光大。等等。

The easiest to understand definition of Aggregate is that it performs an operation on each element of the list taking into account the operations that have gone before. That is to say it performs the action on the first and second element and carries the result forward. Then it operates on the previous result and the third element and carries forward. etc.

示例1合计数值

var nums = new[]{1,2,3,4}; var sum = nums.Aggregate( (a,b) => a + b); Console.WriteLine(sum); // output: 10 (1+2+3+4)

这增加了 1 和 2 ,使 3 。然后添加 3 (的previous结果)和 3 (按顺序下一个元素),使 6 。然后添加 6 和 4 ,使 10 。

This adds 1 and 2 to make 3. Then adds 3 (result of previous) and 3 (next element in sequence) to make 6. Then adds 6 and 4 to make 10.

示例2.从字符串数组

var chars = new []{"a","b","c", "d"}; var csv = chars.Aggregate( (a,b) => a + ',' + b); Console.WriteLine(csv); // Output a,b,c,d

这工作在大致相同的方式。并置 A 一个逗号和 B ,使 A,B 。然后会连接 A,B 用逗号和 C ,使 A,B,C 。等。

This works in much the same way. Concatenate a a comma and b to make a,b. Then concatenates a,b with a comma and c to make a,b,c. and so on.

示例3.使用种子数乘以

有关完整,有总结这需要一个种子值。

For completeness, there is an overload of Aggregate which takes a seed value.

var multipliers = new []{10,20,30,40}; var multiplied = multipliers.Aggregate(5, (a,b) => a * b); Console.WriteLine(multiplied); //Output 1200000 ((((5*10)*20)*30)*40)

就像上​​面的例子中,这始于 5 的值和序列的第一个元素相乘 10 给出的结果 50 。这一结果发扬光大,并乘以下一个数字序列中 20 给的结果,1000年。这继续通过该序列的其余2元素

Much like the above examples, this starts with a value of 5 and multiplies it by the first element of the sequence 10 giving a result of 50. This result is carried forward and multiplied by the next number in the sequence 20 to give a result of 1000. This continues through the remaining 2 element of the sequence.

活生生的实例: rextester/ZXZ64749 文档:msdn.microsoft/en-us/library/bb548651.aspx

Live examples: rextester/ZXZ64749 Docs: msdn.microsoft/en-us/library/bb548651.aspx

附录

例2,上面使用字符串连接创建一个逗号分隔值的列表。这是一个简单的方法来解释使用总结的这是这个答案的意图。但是,如果使用这种技术实际创建了大量的逗号分隔的数据,这将是使用比较合适的StringBuilder ,而这与总结使用种子重载启动的StringBuilder 。

Example 2, above, uses string concatenation to create a list of values separated by a comma. This is a simplistic way to explain the use of Aggregate which was the intention of this answer. However, if using this technique to actually create a large amount of comma separated data, it would be more appropriate to use a StringBuilder, and this is entirely compatible with Aggregate using the seeded overload to initiate the StringBuilder.

var chars = new []{"a","b","c", "d"}; var csv = chars.Aggregate(new StringBuilder(), (a,b) => { if(a.Length>0) a.Append(","); a.Append(b); return a; }); Console.WriteLine(csv);

更新例如: rextester/YZCVXV6464

更多推荐

LINQ聚合算法解释

本文发布于:2023-11-29 22:48:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647851.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   LINQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!