C# and .NET • Modern C# baseline
LINQ Performance and Query Boundaries
Know when LINQ is in-memory and when a provider translates expressions elsewhere.
IEnumerable LINQ runs in memory, while IQueryable providers may translate expression trees to SQL or another query language. A translation boundary changes which methods are supported and where work executes. Avoid accidentally loading an entire table before filtering, and inspect generated queries for data-heavy paths.
Key points
- Apply filters and projections before materializing large query results.
- Avoid calling ToList repeatedly inside loops.
Example
using System.Linq;
IQueryable<Order> query = db.Orders
.Where(order => order.IsOpen)
.OrderBy(order => order.CreatedAt)
.Take(50);
var page = await query.ToListAsync();
What to expect
A provider-backed query requests a limited set of open orders; ToListAsync requires the provider package.
Practice exercise
Compare applying Where before and after materialization. Note which version sends less data over the network.
Key idea: Be explicit about where a query executes and how much data crosses the boundary.