Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

c# - System.Linq.IQueryable does not contain a definition for 'Where'

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

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:

'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?)

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.

question from:https://stackoverflow.com/questions/7489848/system-linq-iqueryable-does-not-contain-a-definition-for-where

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...