【问题标题】:Aggregate and ToArray function in System.Array not workingSystem.Array 中的聚合和 ToArray 函数不起作用
【发布时间】:2014-07-02 19:59:53
【问题描述】:

我有这两个错误:

“System.Array”不包含“Aggregate”的定义,也没有 扩展方法“聚合”接受类型的第一个参数 可以找到“System.Array”(您是否缺少 using 指令或 程序集参考?)

'System.Collections.Generic.IEnumerable' 不包含 'ToArray' 的定义并且不接受扩展方法 'ToArray' 类型的第一个参数 'System.Collections.Generic.IEnumerable' 可以找到 (您是否缺少 using 指令或程序集引用?)

这是我的代码:

    /*
     * Get all the possible permutations
     */
    public static IEnumerable<object[]> CartesianProduct(params object[][] inputs)
    {
        //ERROR: Function Aggregate is not recognized
        return inputs.Aggregate(
            (IEnumerable<object[]>)new object[][] { new object[0] },
            (soFar, input) =>
                from prevProductItem in soFar
                from item in input
                select prevProductItem.Concat(new object[] { item }).ToArray());
    }

    public void test()
    {
            //Get all the posible permutations between parents values.
            var cartesianProduct = CartesianProduct(parentsValues);
            object[][] producto = cartesianProduct.ToArray();
            //ERROR: Function ToArray is not recognized
    }

【问题讨论】:

    标签: c# .net .net-4.5 ienumerable system.array


    【解决方案1】:

    你不见了

    using System.Linq;
    

    在文件的顶部。没有这个,C# 编译器不知道在哪里可以找到您尝试使用的 LINQ 扩展。

    【讨论】:

    • 在我的测试中,它不依赖于 .Net 的版本(除非你回到没有 LINQ 的版本 - 但 3.5 很好),只取决于是否包含 using System.Linq;。也许您正在通过在具有不同usings 的文件之间剪切和粘贴方法来测试不同的版本?如果您可以显示example,则答案将很清楚,或者我们可以发现一些关于 C# 的晦涩难懂的事情/错误。
    【解决方案2】:

    在 .cs 文件顶部添加using System.Linq;

    【讨论】:

      猜你喜欢
      • 2014-11-23
      • 1970-01-01
      • 2020-08-02
      • 2016-04-14
      • 2013-06-06
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多