LyraLearn AI Learning Platform
Exams
← Module 1 Β· Modern C# for MVC Developers
🎧 Listen

LINQ and Collections

LINQ is how C# developers query anything β€” in-memory lists, XML, and (through Entity Framework) SQL Server. The API looks identical everywhere, which is exactly why it bites people: the same line of code can run instantly in memory or fire a database query, depending on one interface.

IEnumerable vs IQueryable β€” the distinction that matters

IEnumerable<T> is a sequence you can iterate. LINQ methods on it (LINQ to Objects) execute as C# code, in memory, over data you already have.

IQueryable<T> is a description of a query β€” an expression tree. LINQ methods on it don't run anything; they build up the description. Entity Framework translates that description into SQL when you enumerate it (.ToList(), foreach, .First(), .Count()). This is deferred execution, and it's the single most important LINQ concept for the EF work later in this course.

The practical consequence: where you put a Where changes what the database does.

db.Orders.Where(o => o.Status == "Open").ToList();   // SQL: WHERE Status = 'Open'
db.Orders.ToList().Where(o => o.Status == "Open");   // SQL: entire table, filter in memory

Both return the same rows. The second one pulls every order across the network first. Keep composing on IQueryable as long as possible; call .ToList() once, at the end, when the query is fully shaped.

The operators you use daily

Pitfall: multiple enumeration

A LINQ query is re-executed every time you enumerate it. This code queries the database twice:

var openOrders = db.Orders.Where(o => o.Status == "Open");
if (openOrders.Any()) ShowCount(openOrders.Count());

For in-memory sequences it means duplicate work; for IQueryable it means duplicate round-trips, and the results can even differ between enumerations. The fix is deliberate materialization: call .ToList() once, store the list, and reuse it. ReSharper's "possible multiple enumeration" warning is worth taking seriously β€” treat any IEnumerable you receive as single-pass unless you made it a list yourself.

🧠 Quiz yourself on this lesson →

Ask the AI Tutor

Grounded in the course lessons β€” it cites its sources and says when it doesn't know.