intermediate20 min

LINQ: Where and Select

Querying a collection declaratively with LINQ's Where and Select methods.

What you'll learn

  • Filter a collection with `Where` and a lambda expression
  • Transform a collection's elements with `Select`
  • Chain LINQ methods together and materialize the result with `ToList()`

Explanation

LINQ (Language Integrated Query) is one of C#'s signature features: a set of methods, available on any IEnumerable<T> (which includes List<T>, arrays, and more), that let you filter, transform, and aggregate data declaratively instead of writing manual loops. LINQ lives in the System.Linq namespace.

Where filters a sequence, keeping only elements matching a condition expressed as a lambda expression -- a compact, inline function: numbers.Where(n => n % 2 == 0) keeps only even numbers. The n => part means "given a parameter n, evaluate the expression that follows."

Select transforms each element into something else: .Select(n => n * n) replaces each number with its square. LINQ methods are commonly chained together in a pipeline: numbers.Where(...).Select(...).

LINQ queries are lazily evaluated by default -- Where/Select don't actually run through the data until something forces it to, like a foreach loop or a call to .ToList(), .Count(), or similar. Calling .ToList() at the end of a chain both forces evaluation and gives you back a concrete List<T> you can index into and reuse.

string.Join(", ", someList) is a handy way to turn any collection into a single readable string for display, joining each element's string representation with the given separator.

Guided lab

Predict: Filtering and transforming with LINQ

C#Not executed
This lab does not run in your browser or on VisaSparkSchools's servers. Read the code, predict what it does, then reveal the real expected output.

Read this program and predict exactly what it prints.

using System;
using System.Linq;
using System.Collections.Generic;

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };

var evenSquares = numbers
    .Where(n => n % 2 == 0)
    .Select(n => n * n)
    .ToList();

Console.WriteLine(string.Join(", ", evenSquares));
Console.WriteLine($"Count: {evenSquares.Count}");

Stuck? Get a hint.

Common mistakes

  • Assuming a LINQ query like `numbers.Where(...)` runs immediately -- it's lazily evaluated and only actually iterates when something consumes it, such as `foreach` or `.ToList()`.
  • Forgetting `using System.Linq;` is required for `Where`/`Select` and the other LINQ extension methods to be available.
  • Writing a manual `foreach` loop with a temporary list to filter and transform data, when `Where(...).Select(...)` expresses the same intent more directly.

Knowledge check

Knowledge check

1. What does `numbers.Where(n => n % 2 == 0)` do?
2. When does a LINQ query built from `Where`/`Select` actually iterate through the underlying data?
3. What does `n => n * n` represent in a LINQ `Select` call?

Takeaway

Chain `Where` (filter) and `Select` (transform) to express data-processing pipelines declaratively -- remember they're lazily evaluated until something like `.ToList()` or `foreach` actually consumes the result.

Summary

LINQ's `Where` and `Select` filter and transform any `IEnumerable<T>` using lambda expressions, chainable into a pipeline that's lazily evaluated until consumed.

References

Your notes

Notes save automatically.