问题描述
我有这个控制台应用程序:
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已包括在内:
我试图在“监视”窗口的调试期间运行以下命令:
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:
}