【问题标题】:Method not found: 'Void LinqKit.Extensions.ForEach找不到方法:'无效 LinqKit.Extensions.ForEach
【发布时间】:2016-10-03 21:53:00
【问题描述】:

在我刚刚完成的 LinqKit 更新之后,我的 Visual Studio 2015 开始在编译时抛出异常:

找不到方法:'Void LinqKit.Extensions.ForEach(System.Collections.Generic.IEnumerable1<!!0>, System.Action1)'。

我已将我的 LinqKit 从 1.1.4.1 版本更新到 1.1.7.1 版本。

我在我的代码中使用了许多 ForEach 语句,但幸运的是,抛出的异常并没有说明导致错误的确切行。异常弹出仅指出包含这些 ForEach 语句的方法调用。

提前致谢。

编辑

我以这样的方式进行了 ForEach 调用:

someCollection.ForEach(s => {
    // something here
});

dictionaryA.ForEach(d => {
    // something here
});

dictionaryB.Keys.ForEach(d => {
    // something here
})

【问题讨论】:

  • 你为什么不使用foreach循环?
  • 源没有定义 ForEach 方法。苏..是的。它不在那里。 github.com/scottksmith95/LINQKit/commit/…
  • 是的,谢谢,这是真正的原因。Visual Studio 2015 需要更新解决方案内所有项目的所有 LinqKit dll,以便在开始构建之前显示实际的编译错误行。

标签: c# .net linq


【解决方案1】:

此方法与 LinqKit 无关,已被删除。

.NET 默认为List<T> 提供.ToList(),所以只需说someCollection.ToList().ForEach(...)

.NET 中没有为 IEnumerable 实现 ToList 的原因是 IEnumerable 应该被认为是不可变的集合,而 ForEach 是一种可变方法,会产生一些副作用。

【讨论】:

    【解决方案2】:

    Chee's Burgers 提供的link 表明,当ForEach 存在时,它只是一种扩展方法,将您的“这里的东西”应用于集合中的每个元素,如下所示:

    /// <summary> Default side-effect style enumeration </summary>
    public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
    {
        foreach (var element in source)
            action(element);
    }
    

    因此您可以自己添加该扩展方法,或者您应该能够按如下方式更新您的代码

    foreach(var s in someCollection)
    {
        // something here
    }
    
    foreach(var d in dictionaryA)
    {
        // something here
    }
    
    foreach(var d in dictionaryB.Keys)
    {
        // something here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多