IEnumerable versus IQueryable
IEnumerable versus IQueryable
IEnumerable<T> exposes a sequence that you can enumerate. IQueryable<T> also exposes a query expression and a provider that decides how to execute it. The difference matters when a filter might run in your application or be translated into a request to a remote data source.
Examples target the proposed C# 14 / .NET 10 console baseline. Each is a separate Program.cs; expected outputs are reasoned, not executed.
Compare the two query shapes
Enumerable.Where accepts a delegate (callable code); Queryable.Where accepts an expression tree (a representation of code). The static type of the receiver and the available overloads determine which is selected. AsQueryable on an array creates a local queryable, not a database connection.
using System;using System.Collections.Generic;using System.Linq;int[] values = { 2, 5, 8 };IEnumerable<int> sequence = values;IQueryable<int> queryable = values.AsQueryable();var local = sequence.Where(n => n > 4);var query = queryable.Where(n => n > 4);Console.WriteLine(string.Join(",", local));Console.WriteLine(string.Join(",", query));Console.WriteLine(query.Expression.NodeType);The queryable retains a method-call expression; both queries nevertheless read the same in-memory array.
Expected output (reasoned, not executed):
5,85,8CallChoose the evaluation boundary
AsEnumerable changes the static view of the sequence so later standard operators use Enumerable. It does not by itself copy the data or execute a remote query. A provider may translate earlier operators, whereas the following delegate runs as items are enumerated locally.
using System;using System.Linq;var query = new[] { 1, 4, 6, 9 }.AsQueryable();var result = query.Where(n => n >= 4).AsEnumerable() .Select(n => $"Item-{n}");Console.WriteLine(string.Join(" ", result));The numeric filter precedes the boundary; the formatting happens afterward. This fixture has no remote provider, so it demonstrates composition, not SQL translation.
Expected output (reasoned, not executed):
Item-4 Item-6 Item-9Distinguish a query from its result
A query variable can describe work that has not happened yet. ToArray forces enumeration and stores the resulting values. Reusing the materialized array avoids repeating the query but does not refresh its membership.
using System;using System.Collections.Generic;using System.Linq;var values = new List<int> { 2, 5 };var query = values.AsQueryable().Where(n => n >= 5);var snapshot = query.ToArray();values.Add(8);Console.WriteLine(string.Join(",", snapshot));Console.WriteLine(string.Join(",", query));The local queryable reads the list again when enumerated; the array contains only the earlier result.
Expected output (reasoned, not executed):
55,8Common mistakes and edge cases
Neither interface guarantees that data is already in memory. IQueryable does not guarantee SQL or even a remote source. A provider can reject an expression; a local AsQueryable test is not a translation test. Avoid moving the boundary before a selective filter on a large remote data source without evaluating the cost.
Practice and recap
For [1,3,7,9], write a queryable filter for values greater than 3, then use AsEnumerable to format each as a label. Add a value to a List<int> source after taking a snapshot and compare the results.
Hint: Use Where before AsEnumerable; take a snapshot with ToArray.
Learning objective: Distinguish in-process evaluation from provider translation. See the accompanying manual practice and knowledge check.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
IEnumerable versus IQueryable — knowledge check
Apply the lesson concepts to the following scenarios.
Which parameter does Queryable.Where normally accept for a predicate?
What does AsQueryable on an int array create?
What does AsEnumerable alone do?
Where should a provider-translatable selective filter usually appear?
What does ToArray change?