LINQ聚合算法解释算法、LINQ

2023-09-02 01:28:38 作者:Crave. [奢求]

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

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.

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

Live examples: http://rextester.com/ZXZ64749 Docs: http://msdn.microsoft.com/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);

更新例如: http://rextester.com/YZCVXV6464