这听起来可能很蹩脚,但我还没能找到一个对聚合的真正好的解释。
好的意思是简短、描述性、全面,并有一个小而清晰的例子。
这听起来可能很蹩脚,但我还没能找到一个对聚合的真正好的解释。
好的意思是简短、描述性、全面,并有一个小而清晰的例子。
当前回答
从Jamiec的回答中学到了很多。
如果只需要生成CSV字符串,可以尝试此操作。
var csv3 = string.Join(",",chars);
这是一个100万个字符串的测试
0.28 seconds = Aggregate w/ String Builder
0.30 seconds = String.Join
源代码在这里
其他回答
一张图片胜过千言万语
提醒: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后跟一个转换其结果的函数,可以更简洁地编写相同的代码。
插图改编自这篇优秀的博客文章。
一个简短而重要的定义可能是这样的:Linq Aggregate扩展方法允许声明一种应用于列表元素的递归函数,其操作数是两个:按元素在列表中的出现顺序排列的元素,一次一个元素,以及上一次递归迭代的结果,或者如果还没有递归,则什么都没有。
通过这种方式,您可以计算数字的阶乘,或连接字符串。
这是关于在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<T,T>,则可以将多个转换添加到List<Func<T,T>中,并使用Aggregate遍历T的每个步骤。
更具体的例子
您需要获取一个字符串值,并通过一系列可以通过编程构建的文本转换对其进行遍历。
var transformationPipeLine = new List<Func<string, string>>();
transformationPipeLine.Add((input) => input.Trim());
transformationPipeLine.Add((input) => input.Substring(1));
transformationPipeLine.Add((input) => input.Substring(0, input.Length - 1));
transformationPipeLine.Add((input) => input.ToUpper());
var text = " cat ";
var output = transformationPipeLine.Aggregate(text, (input, transform)=> transform(input));
Console.WriteLine(output);
这将创建一系列转换:删除前导和尾随空格->删除第一个字符->删除最后一个字符->转换为大写。可以根据需要添加、删除或重新排序此链中的步骤,以创建所需的任何类型的转换管道。
这个特定管道的最终结果是“猫”变成了“A”。
一旦你意识到T可以是任何东西,它就会变得非常强大。这可以用于图像转换,如过滤器,使用BitMap作为示例;