To answer the title of the question, you can generally use AsEnumerable
to make subsequent bits execute locally:
var query = db.People
.Where(p => p.Age > 18) // In database
.AsEnumerable()
.Where(p => p.Name == "Jon") // In .NET
The call to AsEnumerable doesn't do anything other than change the static type from IQueryable<T>
to IEnumerable<T>
so that the extension methods in Enumerable
get called instead of those in Queryable
.
However, if you reuse a query that's already executed, it will still execute again in the database... so (to answer the body of the question) if you want to reuse a query's results, you need to convert them into a list first, e.g.
var results = db.People
.Where(p => p.Age > 18)
.ToList();
Then accessing results
won't go back to the database (unless it needs to fetch child values, of course). So you can do:
var subquery = results.Where(p => p.Name == "Jon");
That will perform the filtering in process instead.