LINQ
LINQ (Language-Integrated Query) lets you query any sequence with methods such as Where and Select, each driven by a lambda. Follow one query over the array {72, 45, 90, 61}: building it runs nothing (deferred execution); a foreach then pulls items through one at a time, Where drops 45, Select adds 5, and the console prints 77, 95, 66. ToList runs the whole query again and keeps the results.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01The program runs var passed = scores.Where(s => s >= 60).Select(s => s + 5); and nothing else yet. How many times has the lambda s => s >= 60 been called?
Where and Select use deferred execution. Building the query only wraps the operators around each other; no item is read and no lambda runs until something enumerates passed, such as foreach or ToList.
02During the foreach, in which order do things happen?
Where and Select stream: each pull asks Select, which asks Where, which asks the array for one item. That item is tested, changed and handed to the loop before the next one is read.
03What does Where do with 45 when its lambda s => s >= 60 returns false?
Where keeps only the items its lambda says true to. For 45 it says false, so Where skips it and reads the next score, 90, within the same pull.
04After the foreach, line 7 calls passed.ToList(). What happens?
passed is a recipe, not stored results, so every enumeration runs it again. ToList runs it now (immediate execution) and keeps the results, 77, 95 and 66, in a List<int>.
05What is the same as from s in scores where s >= 60 select s + 5?
Query syntax is translated by the compiler into method calls in the same order: the where clause becomes Where with the lambda s => s >= 60, and select becomes Select with s => s + 5.
Glossary
- LINQ
- Language-Integrated Query: a set of methods in System.Linq, such as Where, Select and ToList, that query any sequence.
- IEnumerable<T>
- The interface for a sequence that hands out its items one at a time. Arrays and List<T> implement it, so LINQ works on them.
- Lambda
- A short unnamed function written with =>, such as s => s >= 60: the input on the left, the result on the right.
- Func<T, TResult>
- A delegate type for a function. Where takes a Func<int, bool> (an int in, true or false out); Select takes a Func<int, int> here.
- Where
- A LINQ operator that keeps only the items for which its lambda returns true.
- Select
- A LINQ operator that turns each item into a new value with its lambda (also called a projection).
- Deferred execution
- A query does nothing when it is built; it runs only when it is enumerated, for example by foreach. It runs again each time.
- ToList
- Runs a query at once and stores its results in a new List<T>, so you can use them without running the query again.
Notes and sources
- The figure shows Where and Select as two machines. For an array, the .NET library may merge Where followed by Select into one iterator object as an optimisation; your lambdas are still called in the same order, one item at a time.
- Where and Select are deferred and streaming: they read one item, act on it, and hand it on. A few operators, such as OrderBy, must read the whole source before they can hand out the first item. ToList, ToArray, Count and First run the query at once.
- Because passed is a recipe, every foreach or ToList reads the source again. If scores changed between the two runs, the second run would see the new values.
- Where and Select are extension methods in System.Linq. New console projects need no "using System.Linq;" line because implicit usings are on (.NET 6 and later).
- The program is real C#. Put the seven lines in Program.cs; dotnet run prints 77, 95, 66 on three lines.
- Microsoft Learn: Language-Integrated Query (LINQ) overview
- Microsoft Learn: Introduction to LINQ queries in C#
- Microsoft Learn: Deferred execution and lazy evaluation
- Microsoft Learn: Write LINQ queries (query syntax and method syntax)
- Microsoft Learn: Enumerable.Where method
- Microsoft Learn: Enumerable.Select method
- Microsoft Learn: Enumerable.ToList method
- Microsoft Learn: Lambda expressions and anonymous functions
- Microsoft Learn: Iteration statements (foreach)
- Microsoft Learn: Standard query operators overview
Quiz Unlocks after the last step.