这里的情况很奇怪。出于某种原因,我无法在我的 IQueryable 对象上调用“Where”或任何其他函数。
这是我所拥有的:
public IQueryable<Employee> Employees
{
get { return _entities.Employees.AsQueryable(); }
}
public ActionResult Index()
{
return View(new HomeViewModel
{
Employees = Employees.Where(e => e.Active == true)
});
}
但是 Intellisense 没有获取 Where 函数,我得到一个构建错误,上面写着:
'System.Linq.IQueryable' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Linq.IQueryable' could be found (are you missing a using directive or an assembly reference?)
但我可以像这样调用 .Where
并且它有效:
public IQueryable<Employee> Employees
{
get { return _entities.Employees.AsQueryable().Where(e => e.Active == true); }
}
我不知道发生了什么。
最佳答案
您需要在它不起作用的文件中添加 using System.Linq;
( documentation ) statement 指令。 IEnumerable/IQueryable 的所有扩展方法都在 Enumerable 中定义和 Queryable类,分别。
为了使用扩展方法,定义方法的类必须在范围内。我的猜测是您的第二个代码片段来自另一个您确实有 using 语句的文件。
关于c# - System.Linq.IQueryable 不包含 'Where' 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7489848/