问题
I'm new to EF so be bit gentle right now I'm using 4.2 I just want to know what is the best way where I can restrict EF to load only filtered data instead of pulling all the data from the db and then applying filters over it.
I can see DbContext.Set() or DbContext.Set().AsQueryable(); not sure about "Where" function but it seems to be working on same principle i.e already loads all the data for any given table and then applies filters over them wouldn't this be a major performance hit? Or am I missing something here? I don't want EF to fetch all the data from table but only filtered one. how to go about it?
thanks
回答1:
Linq queries are translated to SQL so if you use .Where() on the query it should be executed on the db site not in your application. Note that if you do something like .ToList()/.ToArray() and then try applying .Where() on top of it the filtering will take place on the client site as .ToList() will force query execution prior to applying the filter
来源:https://stackoverflow.com/questions/9362145/ef-dbcontext-sett-filtered-record-only