Skip to content

C# LINQ Where — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about C# LINQ Where. We cover key concepts, practical examples, and best practices.

You have a list of items and need only those matching a condition. You write a foreach loop with an if statement, adding matching items to a new list. The LINQ Where method expresses the same filter as a declarative lambda.

Wrong

var result = new List<int>();
foreach (var item in numbers)
{
    if (item > 10)
        result.Add(item);
}

Output: Works. Five lines of imperative code for a simple filter.

var result = numbers.Where(n => n > 10).ToList();

Output: Same list, one line. The lambda n => n > 10 is the predicate — each element is tested against it.

Where is a deferred execution method — the predicate is applied lazily as you iterate. Multiple Where calls compose into a single logical filter:

var filtered = numbers
    .Where(n => n > 0)
    .Where(n => n % 2 == 0)
    .ToList();
// Equivalent to: numbers.Where(n => n > 0 && n % 2 == 0)

Prevention

  • Use Where instead of foreach + if for filtering.
  • Chain multiple Where calls for readability — the compiler merges them.
  • Use .ToList() or .ToArray() to materialize if you need the result immediately.
  • Use Where with index: .Where((item, index) => item > index).
  • Use OfType<T>() instead of Where(x => x is T) when filtering by type.
  • Remember that Where does not modify the source — it returns a new query.

Common Mistakes with linq where

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world CSHARP code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

Is Where lazy or eager?

Where is lazy (deferred execution). The predicate is not evaluated until you iterate the result (via foreach, ToList, ToArray, etc.). Each time you iterate, the predicate is re-evaluated against the source.

Can I use Where with IQueryable for database queries?

Yes. Where on IQueryable<T> translates to SQL WHERE clause. The predicate is converted to an expression tree and sent to the database server. This is the primary way to filter data server-side in EF Core.

What is the difference between Where and TakeWhile?

Where tests every element against the predicate. TakeWhile takes elements while the predicate is true and stops at the first false. Where scans the entire sequence; TakeWhile stops early. Use TakeWhile when the sequence is sorted and you know when to stop.

Where is used throughout DodaTech applications for filtering — from log entries in Durga Antivirus Pro to messages in Doda Browser. For more LINQ, visit DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro