What is LINQ in C# and how does deferred execution work?
Understand LINQ in C# and how deferred execution works: lazy queries, when they run, materializing with ToList, and re-execution pitfalls with examples.
Expected Interview Answer
LINQ (Language Integrated Query) is a set of C# features for querying collections, databases, XML and other data sources using a uniform, strongly typed syntax; deferred execution means a query is not run when it is defined but only when its results are actually enumerated.
LINQ operators like Where, Select and OrderBy return an IEnumerable (or IQueryable) that describes the query rather than the results. The pipeline runs lazily the moment you iterate it with foreach or force it with ToList, ToArray, Count, First or similar. This lets you compose queries in stages, and it means the query re-runs each time you enumerate it unless you materialize the results into a concrete collection.
- Uniform query syntax across many data sources
- Strong typing and compile-time checking
- Lazy evaluation avoids unnecessary work
- Composable query pipelines built in stages
- Only the data you consume is processed
AI Mentor Explanation
LINQ is like planning your batting shots: 'if the ball is short, pull it'. Deferred execution means you have not swung yet — the plan exists but nothing happens until a ball actually arrives. Each delivery re-triggers the plan freshly, just as enumerating a LINQ query re-runs it, unless you commit the outcome to the scorebook by materialising it.
Step-by-Step Explanation
Step 1
Build the query
Chain operators like Where and Select; this returns an IEnumerable describing the query, not the results.
Step 2
Understand it is lazy
No data is touched yet — the query object just holds the recipe for producing results.
Step 3
Enumerate to execute
A foreach loop, or a terminal operator, pulls items through the pipeline one at a time.
Step 4
Force with materializers
Call ToList, ToArray, Count or First to run the query immediately and capture results.
Step 5
Beware re-execution
Enumerating the same query variable again re-runs the whole pipeline against current data.
What Interviewer Expects
- Definition of LINQ as language-integrated querying
- Difference between deferred and immediate execution
- Knowing which operators force execution (ToList, Count, First)
- Awareness that queries re-run on each enumeration
- Understanding of streaming vs buffered operators
Common Mistakes
- Thinking the query runs when it is written
- Not realizing a query re-executes on every enumeration
- Assuming ToList and iterating twice have the same cost
- Modifying the source after defining but before executing a query
- Confusing deferred execution with query caching
Best Answer (HR Friendly)
“LINQ is C#'s built-in way to ask questions of your data, like filtering or sorting a list, using one readable syntax. Deferred execution means the question is not actually answered until you look at the results, so nothing is computed until it is needed.”
Code Example
var numbers = new List<int> { 1, 2, 3 };
// Query is defined but NOT run yet (deferred)
var query = numbers.Where(n => n > 1);
numbers.Add(4); // added before enumeration
foreach (var n in query)
Console.WriteLine(n); // 2, 3, 4 — reflects the added item
// ToList forces immediate execution and snapshots results
var snapshot = numbers.Where(n => n > 1).ToList();Follow-up Questions
- Which LINQ operators force immediate execution?
- What is the difference between streaming and buffering operators?
- How does deferred execution affect performance in loops?
- What happens if you modify the source collection mid-query?
- How does LINQ to Objects differ from LINQ to SQL?
MCQ Practice
1. When does a deferred LINQ query actually execute?
Deferred queries run only when enumerated, for example by a foreach loop or a terminal operator.
2. Which method forces immediate execution?
ToList materializes the query into a concrete list right away; Where, Select and OrderBy are deferred.
3. Enumerating the same query variable twice will?
A deferred query re-executes its whole pipeline against current source data each time it is enumerated.
Flash Cards
What does LINQ stand for? — Language Integrated Query.
What is deferred execution? — A query runs only when enumerated, not when defined.
Name three operators that force execution. — ToList, ToArray, Count (also First, Sum, etc.).
Why can a query give different results twice? — It re-runs against current source data on each enumeration.
How do you snapshot results? — Materialize with ToList or ToArray to capture them immediately.