C# Program to Find the List of Students whose Name Starts with ‘S’ using where() Method of List Collection using LINQ

The Where() method in LINQ is a powerful filtering tool that allows you to query collections based on specific conditions. This article demonstrates how to use the Where() method with a List<T> collection to find students whose names start with the letter 'S'.

LINQ (Language Integrated Query) is a .NET framework feature that provides a consistent way to query different data sources using C# syntax. It eliminates the need for separate query languages like SQL when working with in-memory collections, making data filtering and manipulation more intuitive.

Syntax

The Where() method has the following syntax

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

Parameters

  • source The IEnumerable<T> collection to filter

  • predicate A function that tests each element for a condition

Return Value

Returns an IEnumerable<T> containing elements that satisfy the predicate condition.

Example

Let's create a program to find chess team members whose names start with 'S'

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

class Student {
    public int Roll { get; set; }
    public string Name { get; set; }
    public int Standard { get; set; }
    
    public override string ToString() {
        return $"{Roll} {Name} {Standard}";
    }
}

class Program {
    static void Main(string[] args) {
        List<Student> students = new List<Student>() {
            new Student { Roll = 21, Name = "Ankit", Standard = 10 },
            new Student { Roll = 12, Name = "Abhinay", Standard = 10 },
            new Student { Roll = 07, Name = "Shubham", Standard = 11 },
            new Student { Roll = 14, Name = "Shreyank", Standard = 12 },
            new Student { Roll = 10, Name = "Shahnawaz", Standard = 11 }
        };
        
        // Using Where() method to filter students whose names start with 'S'
        var studentsWithS = students.Where(s => s.Name.StartsWith("S"));
        
        Console.WriteLine("Students whose names start with 'S':");
        Console.WriteLine("Roll Name     Standard");
        Console.WriteLine("------------------------");
        
        foreach (Student student in studentsWithS) {
            Console.WriteLine(student.ToString());
        }
    }
}

The output of the above code is

Students whose names start with 'S':
Roll Name     Standard
------------------------
7 Shubham 11
14 Shreyank 12
10 Shahnawaz 11

Using Different Filtering Approaches

You can also filter using character indexing or other string methods

Using Character Index

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

class Program {
    static void Main(string[] args) {
        List<string> names = new List<string> { "Sam", "Alice", "Steve", "Bob", "Sarah" };
        
        // Filter names starting with 'S' using character indexing
        var filteredNames = names.Where(name => name[0] == 'S');
        
        Console.WriteLine("Names starting with 'S':");
        foreach (string name in filteredNames) {
            Console.WriteLine(name);
        }
    }
}

The output of the above code is

Names starting with 'S':
Sam
Steve
Sarah

Key Features of Where() Method

Feature Description
Deferred Execution Query is executed only when enumerated
Chainable Can be combined with other LINQ methods
Type Safety Compile-time checking prevents runtime errors
Flexibility Supports complex predicates and lambda expressions

Conclusion

The Where() method in LINQ provides an elegant and efficient way to filter collections based on specified conditions. It integrates seamlessly with C# syntax, making data querying intuitive and type-safe while supporting complex filtering scenarios through lambda expressions.

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

605 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements