哪里"折叠" LINQ扩展方法? [英] Where is the "Fold" LINQ Extension Method?

查看:133
本文介绍了哪里"折叠" LINQ扩展方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现的LINQ的样本一个名为折叠整​​齐的方法(),我要使用。他们的例子:

I found in MSDN's Linq samples a neat method called Fold() that I want to use. Their example:

double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; 
double product = 
     doubles.Fold((runningProduct, nextFactor) => runningProduct * nextFactor);



不幸的是,我不能得到这个编译,无论是在他们的榜样还是在我自己的代码,我不能找到其他地方在MSDN(如可枚举或阵列扩展方法)中提到这种方法。我得到的错误是一个普通的老不知道呢错误:

Unfortunately, I can't get this to compile, either in their example or in my own code, and I can't find anywhere else in MSDN (like Enumerable or Array extension methods) that mention this method. The error I get is a plain old "don't know anything about that" error:

error CS1061: 'System.Array' does not contain a definition for 'Fold' and no 
extension method 'Fold' accepting a first argument of type 'System.Array' could 
be found (are you missing a using directive or an assembly reference?)

我用其他的方法,我相信来自LINQ的(如选择()和Where ()),而我的使用System.Linq的,所以我认为这是一切OK。

I'm using other methods which I believe come from Linq (like Select() and Where()), and I'm "using System.Linq", so I think that's all OK.

请问这个方法是否真的存在在C#3.5,如果是这样,我究竟做错了什么?

Does this method really exist in C# 3.5, and if so, what am I doing wrong?

推荐答案

您将要使用总结扩展方法:

double product = doubles.Aggregate(1.0, (prod, next) => prod * next);

请参阅的MSDN 了解更多信息。它可以让你指定种子,然后一个计算连续值表达式。

See MSDN for more information. It lets you specify a seed and then an expression to calculate successive values.

这篇关于哪里"折叠" LINQ扩展方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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