我试着想出最好的方法来说出这个来解决我的确切问题,而不必有人解释Aggregate的作用,因为我知道这里已经深入讨论了这里和互联网上的其他地方.调用Aggregate()并使用类似的linq语句时
(a,b) => a+b
Run Code Online (Sandbox Code Playgroud)
什么是a,什么是b?我知道a是当前的元素,但是b是什么?我已经看过一些例子,看起来b只是一个元素,而不是其他例子,它看起来像是前一个函数和其他例子的结果,似乎b是前一个函数的结果.
我在http://msdn.microsoft.com/en-us/library/bb548744.aspx 这里查看了实际C#文档页面上的示例 ,这里是 http://www.dotnetperls.com/aggregate
但我只需要澄清linq表达式中两个参数之间的区别.如果我错过了一些基本的Linq知识来回答这个问题,请随意把我放在我的位置.
请查看http://msdn.microsoft.com/en-us/library/bb548651.aspx上的示例
string sentence = "the quick brown fox jumps over the lazy dog";
// Split the string into individual words.
string[] words = sentence.Split(' ');
// Prepend each word to the beginning of the
// new sentence to reverse the word order.
string reversed = words.Aggregate((workingSentence, next) =>
next + " " + workingSentence);
Console.WriteLine(reversed);
// This code produces the following output:
//
// dog lazy the over jumps fox brown quick the
Run Code Online (Sandbox Code Playgroud)
在此示例中,传递给的匿名函数Aggregate是(workingSentence, next) => next + " " + workingSentence. a将workingSentence包含聚合到当前元素的结果,并且b将是添加到聚合的当前元素.在第一次调用匿名函数时,workingSentence = ""和next = "the".在接下来的电话中,workingSentence = "the"和next = "quick".
| 归档时间: |
|
| 查看次数: |
547 次 |
| 最近记录: |