来。IQueryable不包含“Where”的定义

时间:2021-02-06 16:57:54

Very odd situation here. For some reason I can't call 'Where', or any other functions, on my IQueryable object.

非常奇怪的情况。出于某种原因,我不能在IQueryable对象上调用Where或其他函数。

Here's what I have:

这就是我有:

public IQueryable<Employee> Employees
{
    get { return _entities.Employees.AsQueryable(); }
}


public ActionResult Index()
{
    return View(new HomeViewModel
        {
            Employees = Employees.Where(e => e.Active == true)
        });
}

But Intellisense doesn't pick up the Where function, and I get a Build Error that says:

但是智能感知并没有选择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?)

”来。IQueryable不包含“Where”的定义,也不包含“Where”的扩展方法“Where”接受System.Linq类型的第一个参数。可以找到“可查询”(您是否遗漏了一个使用指令或程序集引用?)

But I can call .Where like this and it works:

但我可以打电话给。

public IQueryable<Employee> Employees
{
    get { return _entities.Employees.AsQueryable().Where(e => e.Active == true); }
}

I have no idea what's going on.

我不知道发生了什么事。

1 个解决方案

#1


51  

You need to add a "using System.Linq;" statement directive in the file where it isn't working. All of the extension methods for IEnumerable/IQueryable are defined in the Enumerable and Queryable classes, respectively.

您需要在文件中添加一个“using System.Linq”语句指令。IEnumerable/IQueryable的所有扩展方法都分别在Enumerable和Queryable类中定义。

In order to use extension methods, the class defining the method must be in scope. My guess is that your second code snippet comes from another file where you do have the using statement.

为了使用扩展方法,定义方法的类必须在范围内。我的猜测是,您的第二个代码片段来自另一个文件,在该文件中您确实有使用语句。

#1


51  

You need to add a "using System.Linq;" statement directive in the file where it isn't working. All of the extension methods for IEnumerable/IQueryable are defined in the Enumerable and Queryable classes, respectively.

您需要在文件中添加一个“using System.Linq”语句指令。IEnumerable/IQueryable的所有扩展方法都分别在Enumerable和Queryable类中定义。

In order to use extension methods, the class defining the method must be in scope. My guess is that your second code snippet comes from another file where you do have the using statement.

为了使用扩展方法,定义方法的类必须在范围内。我的猜测是,您的第二个代码片段来自另一个文件,在该文件中您确实有使用语句。