这听起来可能很蹩脚,但我还没能找到一个对聚合的真正好的解释。
好的意思是简短、描述性、全面,并有一个小而清晰的例子。
这听起来可能很蹩脚,但我还没能找到一个对聚合的真正好的解释。
好的意思是简短、描述性、全面,并有一个小而清晰的例子。
当前回答
这是关于在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);
其他回答
这是关于在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);
这在一定程度上取决于你所说的过载,但基本的想法是:
以种子作为“当前值”开始重复序列。对于序列中的每个值:应用用户指定的函数将(currentValue,sequenceValue)转换为(nextValue)设置当前值=下一个值返回最终currentValue
你可能会发现我的Edulinq系列中的Aggregate文章很有用——它包含了更详细的描述(包括各种重载)和实现。
一个简单的例子是使用聚合作为计数的替代:
// 0 is the seed, and for each item, we effectively increment the current value.
// In this case we can ignore "item" itself.
int count = sequence.Aggregate(0, (current, item) => current + 1);
或者可能将字符串序列中的所有字符串长度相加:
int total = sequence.Aggregate(0, (current, item) => current + item.Length);
就我个人而言,我很少发现聚合有用——“定制”的聚合方法通常对我来说足够好。
一张图片胜过千言万语
提醒:Func<X, Y R> 是一个具有两个输入类型X和Y的函数,返回类型R的结果。
可枚举。聚合有三个重载:
过载1:
A Aggregate<A>(IEnumerable<A> a, Func<A, A, A> f)
例子:
new[]{1,2,3,4}.Aggregate((x, y) => x + y); // 10
这种过载很简单,但有以下限制:
序列必须包含至少一个元素,否则函数将抛出InvalidOperationException。元素和结果的类型必须相同。
过载2:
B Aggregate<A, B>(IEnumerable<A> a, B bIn, Func<B, A, B> f)
例子:
var hayStack = new[] {"straw", "needle", "straw", "straw", "needle"};
var nNeedles = hayStack.Aggregate(0, (n, e) => e == "needle" ? n+1 : n); // 2
这种过载更为普遍:
必须提供种子值(bIn)。集合可以是空的,在这种情况下,函数将产生种子值作为结果。元素和结果可以具有不同的类型。
过载3:
C Aggregate<A,B,C>(IEnumerable<A> a, B bIn, Func<B,A,B> f, Func<B,C> f2)
第三个过载不是很有用的IMO。通过使用重载2后跟一个转换其结果的函数,可以更简洁地编写相同的代码。
插图改编自这篇优秀的博客文章。
释义
聚合方法是泛型集合的扩展方法。聚合方法将函数应用于集合的每个项。不仅应用函数,还将其结果作为下一次迭代的初始值。因此,我们将从集合中获得计算值(最小值、最大值、平均值或其他统计值)。
因此,聚合方法是递归函数的一种安全实现形式。
安全,因为递归将在集合的每个项上迭代,我们无法通过错误的退出条件获得任何无限循环暂停。递归,因为当前函数的结果用作下一个函数调用的参数。
语法:
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
用于对多维整数数组中的列求和的聚合
int[][] nonMagicSquare =
{
new int[] { 3, 1, 7, 8 },
new int[] { 2, 4, 16, 5 },
new int[] { 11, 6, 12, 15 },
new int[] { 9, 13, 10, 14 }
};
IEnumerable<int> rowSums = nonMagicSquare
.Select(row => row.Sum());
IEnumerable<int> colSums = nonMagicSquare
.Aggregate(
(priorSums, currentRow) =>
priorSums.Select((priorSum, index) => priorSum + currentRow[index]).ToArray()
);
在Aggregate函数中使用带索引的Select对匹配的列求和并返回新的Array;{ 3 + 2 = 5, 1 + 4 = 5, 7 + 16 = 23, 8 + 5 = 13 }.
Console.WriteLine("rowSums: " + string.Join(", ", rowSums)); // rowSums: 19, 27, 44, 46
Console.WriteLine("colSums: " + string.Join(", ", colSums)); // colSums: 25, 24, 45, 42
但是,由于累积类型(int)与源类型(bool)不同,因此计算布尔数组中的true数更为困难;这里需要种子以使用第二过载。
bool[][] booleanTable =
{
new bool[] { true, true, true, false },
new bool[] { false, false, false, true },
new bool[] { true, false, false, true },
new bool[] { true, true, false, false }
};
IEnumerable<int> rowCounts = booleanTable
.Select(row => row.Select(value => value ? 1 : 0).Sum());
IEnumerable<int> seed = new int[booleanTable.First().Length];
IEnumerable<int> colCounts = booleanTable
.Aggregate(seed,
(priorSums, currentRow) =>
priorSums.Select((priorSum, index) => priorSum + (currentRow[index] ? 1 : 0)).ToArray()
);
Console.WriteLine("rowCounts: " + string.Join(", ", rowCounts)); // rowCounts: 3, 1, 2, 2
Console.WriteLine("colCounts: " + string.Join(", ", colCounts)); // colCounts: 3, 2, 1, 2