How does the following LINQ statement work?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
LINQ (Language Integrated Query) is a C# feature that provides a unified syntax for querying data from various sources — arrays, lists, databases, XML, and more. LINQ statements come in two forms: query syntax (SQL-like from...where...select) and method syntax (chained extension methods like .Where().Select()). Both compile to the same underlying method calls. Understanding how LINQ works means understanding deferred execution, lambda expressions, and the IEnumerable<T> / IQueryable<T> interfaces.
Query Syntax Explained
A typical LINQ query syntax statement:
Breaking it down:
from n in numbers— declares a range variablenthat iterates over each element innumberswhere n % 2 == 0— filters elements, keeping only those where the condition is trueselect n— specifies what to return for each matching element
Equivalent Method Syntax
The compiler translates query syntax into method syntax:
For more complex queries:
Deferred Execution
LINQ queries are not executed when defined — they execute when iterated:
The query re-executes each time it is iterated. To capture results at a specific point, materialize with .ToList() or .ToArray():
Common LINQ Operations
Filtering
Projection (Transform)
Ordering
Aggregation
Grouping
Joining
How the Compiler Processes LINQ
The compiler transforms query syntax into extension method calls defined in System.Linq.Enumerable (for IEnumerable<T>) or System.Linq.Queryable (for IQueryable<T>):
First, Single, and Default Variants
Common Pitfalls
- Multiple enumeration of a deferred query: Each
foreachor LINQ operation on a deferred query re-executes it. If the query is expensive (database call), materialize it with.ToList()first to avoid redundant execution. - Confusing
First()withSingle():First()returns the first match and ignores duplicates.Single()throws if there are zero or multiple matches. UseSingle()when exactly one result is expected. - Modifying a collection during LINQ iteration: Adding or removing elements from a
List<T>while iterating withforeachthrowsInvalidOperationException. Materialize with.ToList()before modifying the source. - Using query syntax for simple operations: Query syntax is verbose for single-method operations like filtering.
numbers.Where(n => n > 5)is cleaner thanfrom n in numbers where n > 5 select nfor simple cases. - Assuming LINQ is always lazy: Methods like
.ToList(),.ToArray(),.Count(),.Sum(),.First(), and.Single()execute the query immediately. Only chained.Where(),.Select(),.OrderBy()are deferred.
Summary
- LINQ query syntax (
from...where...select) compiles to extension method calls (.Where().Select()) - Queries use deferred execution — they run when iterated, not when defined
- Use
.ToList()or.ToArray()to materialize results and prevent re-execution IEnumerable<T>LINQ runs in memory;IQueryable<T>LINQ translates to SQL via expression trees- Both query syntax and method syntax produce identical results — choose based on readability

