IQueryable vs IEnumerable: How Your LINQ Choice Decides Where Filtering Happens
In C#, IEnumerable and IQueryable share identical LINQ syntax but execute filtering in fundamentally different places — memory versus database. IEnumerable processes data inside the application after fetching all rows, while IQueryable builds an expression tree that gets translated into SQL and executed at the database level. A common anti-pattern involves returning IEnumerable from a repository method, which silently forces the entire table into memory before any filtering occurs, causing severe performance issues at scale. Developers are advised to keep IQueryable as long as possible in the query chain and only switch to IEnumerable via AsEnumerable() when applying custom C# logic that cannot be translated to SQL. Calling ToList() or AsEnumerable() marks the boundary where database execution ends and in-memory processing begins.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in