LINQ Interview Question #91
What is the difference between IQueryable and materialized data?
Deferred Execution, Immediate Execution & Query Behavior Mid-Level Intermediate
Quick Interview Answer
IQueryable<T> represents a query definition that a provider may continue translating. A materialized List<T> or array contains actual in-memory results that have already been retrieved.
Detailed Explanation
IQueryable<T> represents a query definition that a provider may continue translating. A materialized List<T> or array contains actual in-memory results that have already been retrieved.
Keeping a database query as IQueryable allows later filters, projections, and ordering to potentially become SQL. Once materialized, later LINQ operations are LINQ to Objects.
Code Example
IQueryable<Product> query =
db.Products.Where(p => p.IsActive);
query = query.Where(p => p.Price > 100);
List<Product> products =
await query.ToListAsync();