What is the difference between IEnumerable<T> and IQueryable<T>?
cs-mid-003
Your answer
Answer as you would in a real interview — explain your thinking, not just the conclusion.
Model answer
IEnumerable<T> pulls data into memory and applies filter/transform operations in-process using LINQ-to-Objects. IQueryable<T> represents an expression tree that a query provider (like Entity Framework) translates to SQL or another query language. The critical difference: filtering on IQueryable runs in the database, while filtering on IEnumerable after materialisation runs in C# on all loaded rows. Calling .ToList() or .AsEnumerable() materialises the IQueryable and switches to LINQ-to-Objects. Calling dbContext.Users.Where(u => SomeComplexCSharpMethod(u)) will throw NotSupportedException because EF cannot translate arbitrary C# methods to SQL.
Code example
// BAD: fetches ALL users, filters in C# memory
var seniors = dbContext.Users
.AsEnumerable()
.Where(u => u.Age > 40)
.ToList();
// GOOD: WHERE clause pushed to SQL, only matching rows returned
var seniors = dbContext.Users
.Where(u => u.Age > 40)
.ToList();
Follow-up
Can you give a real example where accidentally switching from IQueryable to IEnumerable caused a performance problem in production?