Both are used with LINQ, but they decide where the query runs. IEnumerable<T> - Lives in System.Collections.Generic and represents an in-memory sequence that can be iterated. - LINQ methods take Func<T, bool> delegates and run as normal C# code after the data has been loaded (LINQ to Objects). IQueryable<T> - Lives in System.Linq and extends IEnumerable<T>. - LINQ methods take Expression<Func<T, bool>>, which builds an expression tree. A provider such as Entity Framework Core translates that tree into SQL and runs it in the database. Why it matters: // IQueryable: filtering and paging happen in SQL var page = await db.Orders .Where(o => o.CustomerId == id) .OrderByDescending(o => o.CreatedAt) .Take(20) .ToListAsync(); // IEnumerable: the whole table is loaded, then filtered in memory IEnumerable<Order> orders = db.Orders; var page2 = orders.Where(o => o.CustomerId == id).Take(20).ToList(); The second version transfers every order row from the database — slow, memory-hungry and dangerous on large tables. Other points: - Both use deferred execution: nothing runs until you enumerate (foreach, ToList, Count). - With IQueryable, only expressions the provider can translate are allowed; calling a custom C# method inside Where may fail or force client evaluation. - Use AsEnumerable() deliberately when you need in-memory processing after the database part of the query is done. - Expose IQueryable carefully: returning it from a repository lets callers build arbitrary queries and leaks persistence concerns. In short: IQueryable for composing database queries, IEnumerable for working with data already in memory.
Ravindra Wadile · · 0 likes