代码之家  ›  专栏  ›  技术社区  ›  Ken

“折叠”LINQ扩展方法在哪里?

  •  81
  • Ken  · 技术社区  · 16 年前

    MSDN's Linq samples 我想使用一个名为Fold()的简洁方法。他们的例子是:

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

    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?)
    

    这种方法真的存在于C#3.5中吗?如果存在,我做错了什么?

    2 回复  |  直到 16 年前
        1
  •  131
  •   Jason    16 年前

    Aggregate

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

    MSDN 了解更多信息。它允许您指定 seed 然后是计算连续值的表达式。

        2
  •  42
  •   Richard Berg    16 年前

    Fold Aggregate

    double product = doubles.Aggregate(1.0, (runningProduct, nextFactor) => runningProduct* nextFactor);