Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท LINQ

Enumerable.Where() method

Reference for C# LINQ Where(): filter a sequence with a predicate, use the index overload, combine conditions, understand deferred execution and null checks.

Where filters a sequence: it returns the elements for which a predicate returns true, in their original order. It is the LINQ equivalent of an if inside a foreach that collects matching items.

Where is lazy. Calling it does not examine any element; the predicate runs as you enumerate the result, one element at a time, and stops early if you stop enumerating (for example with First() or Take(2)).

C#
int[] numbers = [1, 2, 3, 4, 5, 6];
var even = numbers.Where(n => n % 2 == 0);   // 2, 4, 6

Try it

C# Filtering with predicates

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Lazy filtering

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static IEnumerable<TSource> Where<TSource>(
    this IEnumerable<TSource> source, Func<TSource, bool> predicate);

public static IEnumerable<TSource> Where<TSource>(
    this IEnumerable<TSource> source, Func<TSource, int, bool> predicate);
Name Description
source The sequence to filter.
predicate A function returning true for elements to keep. The second overload also receives the zero-based index of the element.

Returns: an IEnumerable<TSource> of the matching elements, in source order. It is evaluated lazily. IQueryable<T> has matching overloads taking Expression<Func<...>>.

Errors: ArgumentNullException if source or predicate is null (thrown immediately). Exceptions thrown by the predicate surface during enumeration.

Usage notes

  • The query form is from x in source where condition select x.
  • Where never changes the source and always returns a new lazy sequence. Because it re-runs on each enumeration, store the result with ToList() when you need it more than once or when the source may change.
  • The index overload is only meaningful if the source order is stable.
  • Combine conditions with && in one predicate for speed and clarity; separate Where calls add one delegate call per element.
  • Put cheap, selective filters first, before sorting or projecting, so later steps process fewer items.
  • To keep only a type, use OfType<T>() (see Cast and OfType) rather than Where(x => x is T), which does not change the sequence's static type.
  • For a plain list, List<T>.FindAll or RemoveAll are alternatives; Where works on any IEnumerable<T>.
  • Filtering on a database IQueryable<T> is translated to SQL WHERE; use only expressions the provider supports.