即使包含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已包括在内:

enter image description here

我试图在“监视”窗口的调试期间运行以下命令:

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

但是我遇到了这个错误:

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

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

解决方法

似乎需要使用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: 
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...