How change ListT data to IQueryableT data
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting a List<T> to IQueryable<T> in C# is done with a single call to .AsQueryable(). However, the resulting IQueryable<T> does not gain database-side query translation — it still evaluates in memory using LINQ to Objects. The conversion is useful for creating testable code, building unified APIs that accept IQueryable<T>, or providing an IQueryable<T> interface over in-memory data for mocking purposes.
Basic Conversion with AsQueryable()
AsQueryable() wraps the list in an EnumerableQuery<T> that implements IQueryable<T> while delegating execution to LINQ to Objects.
IQueryable vs IEnumerable vs List
| Feature | List<T> | IEnumerable<T> | IQueryable<T> |
| Storage | In-memory | Deferred | Deferred |
| Execution | Immediate | Client-side | Provider-dependent |
| Expression trees | No | No | Yes |
| Can translate to SQL | No | No | Yes (with EF/LINQ provider) |
When AsQueryable() Is Useful
Unit Testing with Mock Data
Unified API That Accepts IQueryable
What AsQueryable() Does NOT Do
AsQueryable() creates expression trees from your LINQ queries, but without a database provider, those expression trees are compiled into delegates and executed in memory — exactly like IEnumerable<T>.
Converting Back: AsEnumerable() and ToList()
Building Dynamic Queries
One advantage of IQueryable<T> is composing queries before execution:
Common Pitfalls
- Expecting database query translation:
list.AsQueryable().Where(...)does NOT generate SQL. The data is already in memory, and all filtering happens client-side. OnlyIQueryable<T>backed by a LINQ provider (like Entity Framework) translates to SQL. - Performance misconceptions: Converting a
List<T>toIQueryable<T>does not improve performance. The underlying data is still iterated in memory. It only changes the API surface toIQueryable<T>. - Test behavior mismatch: LINQ to Objects (from
AsQueryable()) supports operations that LINQ to SQL does not (e.g.,StringComparisonoverloads, custom method calls). A test passing withAsQueryable()may fail against a real database. - Deferred execution surprises:
AsQueryable()queries are deferred — they execute when enumerated. If the sourceList<T>is modified between creating the query and enumerating it, the results reflect the modified list. - Using AsQueryable() on DbSet: Calling
AsQueryable()on an Entity FrameworkDbSet<T>is redundant —DbSet<T>already implementsIQueryable<T>. The extra call has no effect.
Summary
- Use
list.AsQueryable()to convertList<T>toIQueryable<T> - The conversion is useful for testing, unified APIs, and dynamic query building
AsQueryable()does NOT enable database query translation — it still uses LINQ to Objects- Only
IQueryable<T>backed by a LINQ provider (Entity Framework, NHibernate) translates queries to SQL - For real deferred database queries, use
DbContext.Set<T>()orDbSet<T>directly

