C# Linq System.Linq 和 System.Collections 之间的模糊调用

标签 c# linq collections

有与此类似的问题 - 但到目前为止都没有帮助。

我在打电话:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

然后在我的代码的一部分中,VS 指出了 LINQ 方法(.Last() 和 .Max())中的错误。我将鼠标悬停在 .Max() 上。错误说:

The call is ambiguous between the following methods or properties: 'System.Linq.Enumerable.Max(System.Collections.Generic.IEnumerable<int>)' and 'System.Linq.Enumerable.Max(System.Collections.Generic.IEnumerable<int>)'

我已经尝试重新安装所有引用资料和软件包,我重新启动了 VS、IIS(这是一个网站)。 VS 识别 System.Linq,所以这不是问题....

我不明白为什么这是突然的抛出错误。

enter image description here

最佳答案

如果您的代码中的任何地方或引用的程序集中有人有实现他们自己的 IEnumerable<T> 的可怕想法,就会发生这种情况。扩展方法并且不知道更好,使用与框架提供的 namespace 相同的 namespace :

  • Alpha 组件自己滚动 IEnumerable<T> System.Linq 中的扩展方法命名空间:

    namespace System.Linq {
        public static class MyEnumerable {
            public static T Max<T>(this IEnumerable<T> source) { //...
            }
        //... 
        }
    }
    
  • 程序集 Charlie 认为 Alpha 很棒,因为它提供了一些其他功能,并且引用它时完全没有注意到它隐藏在里面的令人讨厌的惊喜。

    using System.Linq; //woops
    
    namespace Charlie {
        class C {
            void Foo() {
                var l = new List<object>() { 1, 2, 3 };
                var m = l.Max() /*compile time error. Ambiguous call*/ } }
    }
    

我不知道这是否会成为您的情况,但这是一种可能的情况。另一个潜在的候选者可能是一些版本控制冲突,但我不确定这在 VS 中是否可能。我从来没有遇到过与 System.* 类似的事情命名空间。

关于C# Linq System.Linq 和 System.Collections 之间的模糊调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54045748/

相关文章:

c# - 如何防止 WPF DataGrid 在项目更新时取消选择 SelectedItem?

c# - "Argument data type ntext is invalid for argument 1 of upper function"尝试将第三个项目添加到过滤器时

c# - 如何从 LINQ 将 Sql DateTime 设置为 null

url - 为 Backbone Collection 使用动态 url

nHibernate 集合计数

collections - 为什么借用的 HashMap 的键和值是通过引用访问的,而不是值?

c# - 显示名称为 'VJSharpCodeProvider' 的程序集加载失败

c# - Libsodium-net - 无法加载 DLL 'libsodium.dll

c# - 如何阻止刷新值?

c# - 当您还选择 EFCore 3.1.11 中的其他对象列表时,左连接不会带来所有结果