简体   繁体   中英

If I have a function that returns AsEnumerable, does it cache all result?

Let's say I have a following method.

public IEnumerable<T> GETEVERYTHING()
{
     return _db.AsEnumerable();
}

then I call the method in my code

GETEVERYTHING().Where(w => w.Active == true);

Does it get everything from the Database, cache it, and filter the result or it will filter the result before calling database. For example : Will it call SELECT * FROM table or SELECT * FROM table WHERE Active = 1 in the database?

Sorry for bad english, any help will be appreciated.

calling

return _db.AsEnumerable();

Does not really get the query to execute since it is an IQueryable, which inherits IEnumerable, so nothing happens. calling .ToList() will however force execution, if that helps at all

EDIT: As pointed out in the comments, calling the where method by itsself should not force an execute MOST of the time.

AsEnumerable is marker that divide linq that will be transfered by query provider and linq that will be executed as delegates. If you write db.Where(w=>w.IsActive==true).AsEnumerable().Where(w=>w.IGood==true) , the first part of query will be transfered to sql code:

select * from dbs where IsActive =1

but second part ( .Where(w=>w.IGood==true) ) will be executed as delegate in c# code when you call ToArray(), ToList() or iterate with foreach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM