LINQ Interview Question #89

What does AsQueryable do?

Deferred Execution, Immediate Execution & Query Behavior Mid-Level Intermediate

Quick Interview Answer

AsQueryable exposes a compatible source through IQueryable<T>. If the source already implements IQueryable<T>, it is returned as a queryable source. For an in-memory IEnumerable<T>, LINQ can provide a queryable wrapper.

Detailed Explanation

AsQueryable exposes a compatible source through IQueryable<T>. If the source already implements IQueryable<T>, it is returned as a queryable source. For an in-memory IEnumerable<T>, LINQ can provide a queryable wrapper.

Calling AsQueryable on an in-memory collection does not turn it into a database query. The underlying provider still determines where and how execution occurs.

Code Example

var numbers = new List<int> { 1, 2, 3 };

IQueryable<int> query =
    numbers.AsQueryable();

var result = query.Where(n => n > 1);