首页 > 解决方案 > 即使包含 System.Core.dll 也无法访问 System.Linq

问题描述

我有这个控制台应用程序:

namespace LinqTestFramework
{
    using System.Collections.Generic;
    
    public class Program
    {
        static void Main(string[] args)
        {
            var dict = new Dictionary<int, string>()
            {
                { 1, "David" }
            };            
        } //breakpoint set here: Watch window: System.Linq.Enumerable.Where(dict, x => true)
    }
}

namespace LinqTestFramework
{
    using System.Linq;
    public class SomeClass
    {
        public void SomeMethod()
        {
            var valuesBelowFour = Enumerable.Range(0, 10).Where(x => x < 4);
        }        
    }
}

我可以从 ILSpy 中看到包含这些方法的 System.Core dll:

在此处输入图像描述

我试图在调试期间从 Watch 窗口运行以下命令:

System.Linq.Enumerable.Where(dict, x => true)

但我收到了这个错误:

错误 CS0234:命名空间“System”中不存在类型或命名空间名称“Linq”(您是否缺少程序集引用?)

为什么我无法访问 中的扩展方法System.Linq.Enumerable

标签: c#linqdll.net-4.7.2

解决方案


似乎它需要一个使用 Linq 方法来加载它的方法调用。所以我尝试了下面的代码,现在它正在工作

public class Program
    {
        static void Main(string[] args)
        {
            var dict = new Dictionary<int, string>()
            {
                { 1, "David" }
            };
            
           var n = dict.Keys.Select(x=>x);

        } //breakpoint set here: Watch window: 
}

推荐阅读