C# Linq Where Method

The Where method in LINQ is used to filter elements from a collection based on a specified condition (predicate). It returns a new IEnumerable<T> containing only the elements that satisfy the given criteria.

Syntax

The Where method has two overloads −

// Filter based on element value only
public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate)

// Filter based on element value and index
public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, int, bool> predicate)

Parameters

  • source − The input sequence to filter
  • predicate − A function that tests each element for a condition

Return Value

Returns an IEnumerable<T> that contains elements from the input sequence that satisfy the condition specified by predicate.

Using Where with Simple Predicate

Here's how to filter numbers greater than or equal to 70 −

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

public class Demo {
    public static void Main() {
        int[] arr = { 10, 30, 20, 15, 90, 85, 40, 75 };
        
        Console.WriteLine("Original Array:");
        foreach (int a in arr)
            Console.WriteLine(a);
        
        // Filter elements above or equal to 70
        IEnumerable<int> filteredNumbers = arr.Where(n => n >= 70);
        
        Console.WriteLine("\nElements >= 70:");
        foreach (int res in filteredNumbers)
            Console.WriteLine(res);
    }
}

The output of the above code is −

Original Array:
10
30
20
15
90
85
40
75

Elements >= 70:
90
85
75

Using Where with Index-Based Filtering

You can also filter based on both element value and its index position −

using System;
using System.Linq;

public class Demo {
    public static void Main() {
        string[] fruits = { "Apple", "Banana", "Cherry", "Date", "Elderberry" };
        
        Console.WriteLine("Original fruits:");
        for (int i = 0; i < fruits.Length; i++)
            Console.WriteLine($"{i}: {fruits[i]}");
        
        // Filter fruits at even indices with length > 5
        var filteredFruits = fruits.Where((fruit, index) => index % 2 == 0 && fruit.Length > 5);
        
        Console.WriteLine("\nFruits at even indices with length > 5:");
        foreach (string fruit in filteredFruits)
            Console.WriteLine(fruit);
    }
}

The output of the above code is −

Original fruits:
0: Apple
1: Banana
2: Cherry
3: Date
4: Elderberry

Fruits at even indices with length > 5:
Elderberry

Using Where with Custom Objects

The Where method works seamlessly with custom objects −

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

public class Student {
    public string Name { get; set; }
    public int Age { get; set; }
    public double GPA { get; set; }
}

public class Demo {
    public static void Main() {
        List<Student> students = new List<Student> {
            new Student { Name = "John", Age = 20, GPA = 3.8 },
            new Student { Name = "Alice", Age = 22, GPA = 3.2 },
            new Student { Name = "Bob", Age = 19, GPA = 3.9 },
            new Student { Name = "Carol", Age = 21, GPA = 2.8 }
        };
        
        // Filter students with GPA above 3.5
        var topStudents = students.Where(s => s.GPA > 3.5);
        
        Console.WriteLine("Students with GPA > 3.5:");
        foreach (var student in topStudents)
            Console.WriteLine($"{student.Name}: {student.GPA}");
    }
}

The output of the above code is −

Students with GPA > 3.5:
John: 3.8
Bob: 3.9

Conclusion

The LINQ Where method provides a powerful and flexible way to filter collections based on custom criteria. It supports both simple value-based filtering and more complex conditions involving element indices, making it an essential tool for data manipulation in C#.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements