这听起来可能很蹩脚,但我还没能找到一个对聚合的真正好的解释。

好的意思是简短、描述性、全面,并有一个小而清晰的例子。


当前回答

从Jamiec的回答中学到了很多。

如果只需要生成CSV字符串,可以尝试此操作。

var csv3 = string.Join(",",chars);

这是一个100万个字符串的测试

0.28 seconds = Aggregate w/ String Builder 
0.30 seconds = String.Join 

源代码在这里

其他回答

释义

聚合方法是泛型集合的扩展方法。聚合方法将函数应用于集合的每个项。不仅应用函数,还将其结果作为下一次迭代的初始值。因此,我们将从集合中获得计算值(最小值、最大值、平均值或其他统计值)。

因此,聚合方法是递归函数的一种安全实现形式。

安全,因为递归将在集合的每个项上迭代,我们无法通过错误的退出条件获得任何无限循环暂停。递归,因为当前函数的结果用作下一个函数调用的参数。

语法:

collection.Aggregate(seed, func, resultSelector);

seed—默认初始值;func-我们的递归函数。它可以是lambda表达式、Func委托或函数类型T F(T result,T nextValue);resultSelector-它可以是一个函数,如func或一个表达式,用于计算、转换、更改和转换最终结果。

工作原理:

var nums = new[]{1, 2};
var result = nums.Aggregate(1, (result, n) => result + n); //result = (1 + 1) + 2 = 4
var result2 = nums.Aggregate(0, (result, n) => result + n, response => (decimal)response/2.0); //result2 = ((0 + 1) + 2)*1.0/2.0 = 3*1.0/2.0 = 3.0/2.0 = 1.5

实际用途:

从数字n中查找阶乘:


int n = 7;
var numbers = Enumerable.Range(1, n);
var factorial = numbers.Aggregate((result, x) => result * x);

其执行与该功能相同的操作:

public static int Factorial(int n)
{
   if (n < 1) return 1;

   return n * Factorial(n - 1);
}

Aggregate()是最强大的LINQ扩展方法之一,如Select()和Where()。我们可以使用它来替换Sum()、Min()。Max()、Avg()功能,或通过实现添加上下文来更改它:

    var numbers = new[]{3, 2, 6, 4, 9, 5, 7};
    var avg = numbers.Aggregate(0.0, (result, x) => result + x, response => (double)response/(double)numbers.Count());
    var min = numbers.Aggregate((result, x) => (result < x)? result: x);

扩展方法的更复杂用法:

    var path = @“c:\path-to-folder”;

    string[] txtFiles = Directory.GetFiles(path).Where(f => f.EndsWith(“.txt”)).ToArray<string>();
    var output = txtFiles.Select(f => File.ReadAllText(f, Encoding.Default)).Aggregate<string>((result, content) => result + content);

    File.WriteAllText(path + “summary.txt”, output, Encoding.Default);

    Console.WriteLine(“Text files merged into: {0}”, output); //or other log info

这是关于在Fluent API(如Linq排序)上使用聚合的说明。

var list = new List<Student>();
var sorted = list
    .OrderBy(s => s.LastName)
    .ThenBy(s => s.FirstName)
    .ThenBy(s => s.Age)
    .ThenBy(s => s.Grading)
    .ThenBy(s => s.TotalCourses);

让我们看看我们想要实现一个接受一组字段的排序函数,这很容易使用Aggregate而不是for循环,如下所示:

public static IOrderedEnumerable<Student> MySort(
    this List<Student> list,
    params Func<Student, object>[] fields)
{
    var firstField = fields.First();
    var otherFields = fields.Skip(1);

    var init = list.OrderBy(firstField);
    return otherFields.Skip(1).Aggregate(init, (resultList, current) => resultList.ThenBy(current));
}

我们可以这样使用:

var sorted = list.MySort(
    s => s.LastName,
    s => s.FirstName,
    s => s.Age,
    s => s.Grading,
    s => s.TotalCourses);

每个人都给出了自己的解释。我的解释是这样的。

聚合方法将函数应用于集合的每个项。例如,让我们使用集合{6,2,8,3}和它执行的函数Add(operator+)(((6+2)+8)+3),并返回19

var numbers = new List<int> { 6, 2, 8, 3 };
int sum = numbers.Aggregate(func: (result, item) => result + item);
// sum: (((6+2)+8)+3) = 19

在本例中,传递了名为Add的方法,而不是lambda表达式。

var numbers = new List<int> { 6, 2, 8, 3 };
int sum = numbers.Aggregate(func: Add);
// sum: (((6+2)+8)+3) = 19

private static int Add(int x, int y) { return x + y; }

从Jamiec的回答中学到了很多。

如果只需要生成CSV字符串,可以尝试此操作。

var csv3 = string.Join(",",chars);

这是一个100万个字符串的测试

0.28 seconds = Aggregate w/ String Builder 
0.30 seconds = String.Join 

源代码在这里

聚合主要用于分组或汇总数据。

根据MSDN“聚合函数在序列上应用累加器函数。”

示例1:将数组中的所有数字相加。

int[] numbers = new int[] { 1,2,3,4,5 };
int aggregatedValue = numbers.Aggregate((total, nextValue) => total + nextValue);

*重要信息:默认情况下,初始聚合值是集合序列中的1元素。即:总变量初始值默认为1。

变量解释

total:它将保存func返回的合计值(聚合值)。

nextValue:它是数组序列中的下一个值。然后将该值添加到聚合值,即总计。

示例2:添加数组中的所有项。还设置初始累加器值,从10开始添加。

int[] numbers = new int[] { 1,2,3,4,5 };
int aggregatedValue = numbers.Aggregate(10, (total, nextValue) => total + nextValue);

参数说明:

第一个参数是初始值(起始值,即种子值),用于开始与数组中的下一个值相加。

第二个参数是一个func,它是一个取2int的func。

1.total:这将与计算后func返回的总和值(聚合值)之前保持相同。

2.nextValue::它是数组序列中的下一个值。然后将该值添加到聚合值,即总计。

此外,调试此代码将使您更好地了解聚合的工作方式。