Linq聚合函数 [英] Linq Aggregate function

查看:73
本文介绍了Linq聚合函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的列表

测试","bla",某物",其他"

"test", "bla", "something", "else"

但是当我在其上使用Aggrate并同时调用一个函数时,在我看来,经过2次迭代"之后,第一个的结果被传递了?

But when I use the Aggrate on it and in the mean time call a function it seems to me that after 2 'iterations' the result of the first gets passed in?

我使用它的方式是:

myList.Aggregate((current, next) => someMethod(current) + ", "+ someMethod(next));

当我在someMethod函数中放置一个断点时,对myList中的信息进行了一些转换,但我注意到在第3次调用之后,我从以前的转换中得到了一个结果作为输入参数.

and while I put a breakpoint in the someMethod function where some transformation on the information in the myList occurs, I notice that after the 3rd call I get a result from a former transformation as input paramter.

推荐答案

这就是它的工作方式.

That's the way its intended to work.

您标记为current的内容,意味着到目前为止已累积的所有内容.在第一次调用时,种子是第一个元素.

What you have labeled current, its meant to be all that have been accumulated so far. On the first call, the seed is the first element.

您可以执行以下操作:

var res = myList
   .Aggregate(String.Empty, (accumulated, next) => accumulated+ ", "+ someMethod(next))
   .Substring(2);//take out the first ", "

那样,您只需在每个元素上应用一次someMethod.

That way, you only apply someMethod once on each element.

这篇关于Linq聚合函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆