How is "return (from tbl in context.[tablename] select tbl)" executed if calling method then does a .Where() on result?

A method returns the IQueryable<T> result of a from x in context.y select x query.

The calling method then calls .Where( a => ...) on that resultset.

Is the entirety of x going to be returned in IQueryable<T>, and then filtered? Or will LINQ load this lazily, and only once the resultset is iterated, will the actual SQL query be executed (using that where condition)?

This is using EntityFramework LINQ to Entities, against a MSSQL database.

Jon Skeet
people
quotationmark

If the method returns an IQueryable<T>, that's returning the query - not the results of the query. So calling Where will construct another query, applying a filter to the original query... still using expression trees.

So when you then actually start iterating over the results, the whole query will be converted to SQL, including the filter.

If the method had returned an IEnumerable<T> instead, then it would still represent the query (rather than the results), but the Where call would be using LINQ to Objects - so the filtering would happen in the client rather than in the database.

people

See more on this question at Stackoverflow