c# - System.Linq.IQueryable 不包含 'Where' 的定义

标签 c# asp.net linq entity-framework

这里的情况很奇怪。出于某种原因,我无法在我的 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/

相关文章:

c# - 名称 '“function”在当前上下文中不存在

c# - Linq 检查 where 子句中的百分比

c# - 解决linq表达式错误

c# - lambda 语法中的 linq 类型声明

c# - NHibernate Linq Select Projection - 使用部分选择检索完整实体

c# - 如何在 C# 中更改 ComboBox 的 itemsSource

C# 图像旋转

asp.net - 如何部署到Azure云服务

c# - .NET Core 上的 Azure Function v2 上的 EF Core 迁移问题

c# - || (或)C# 中的 Linq 运算符