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


Introduction

The topic to learn in this article is writing a C# Program to Find the List of Students whose Name Starts with 'S' using the where() Method of List Collection using LINQ.

Language Integrated Query aka LINQ is used to generate queries in C# language. Previously we had to use other relational languages like SQL and XML. It provides more power to the C# language or any other .NET language. The syntax used to query the database in LINQ is the same as that of querying for data stored in an array.

Before we proceed further and understand the algorithm and code of the C# Program to Find the List of Students whose Name Starts with 'S' using the where() Method of List Collection using LINQ. Let us brush up on LINQ on shorthand.

Language Integrated Query (LINQ)

A part of the.NET framework called LINQ makes it easier for users to retrieve data in a typesafe way. This was introduced in the .NET 3.5 version.

The best feature of LINQ is that it offers a single approach for gaining access to data from many sources, including databases and XML documents. Users may write code that is a lot easier to comprehend, much more succinct, and much more aesthetically pleasing with the aid of LINQ. It also provides other functionalities like filtering, sorting, grouping data and even changing them.

Now, it is time to give some light on the where() method or also known as Enumerable.Where() method falls under the namespace System.Linq.

Where<TSource>(IEnumerable<TSources>,Func<T Source,Boolean>)

This method falls under the namespace System.Linq. This filters a sequence of values based on a predicate. Let us see its defined syntax −

public static System.Collections.Generic.IEnumerable<TSource> Where<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);

This method has the type parameter TSource which tells about the type of elements of the source. Another two parameters are the source which is An IEnumerable<T> to filter and the predicate is the function testing each element for a condition.

This is returning method that returns a collection of items from the input sequence that meet the criteria, represented as an IEnumerable<T>. ArgumentNullException can be thrown when the predicate or source is null.

We are going to take a problem statement which will make it very easy to understand the concept of the where() method.

In a school, there is a chess team that consists of 5 members from different classes. Ankit, Abhinay, Shubham, Shreyank, Shahnawaz. They study in a different classes. A blazer with the initials 'S' is to be provided. So, for knowing the names of the student whose names start with 'S'. The headmaster has tasked you to write a program that selects the student name which starts with 'S'.

Now in the next section, we are going to discuss the algorithm to write C# Program to Find the List of Students whose Name Starts with 'S' using the where() Method of List Collection using LINQ.

Algorithm

Following are the steps that need to be followed while writing the code of the program using the where() method.

Step 1 − Remember to declare the proper namespace for using the where() method i.e., System.Linq.

Step 2  Start the class code and declare three variables to store the roll number, class, and name of the student.

Step 3  In the next step we create the string method which returns the roll number, name, and standard of the student.

Step 4  In the main() part we declare the list variable and then add the data to the list.

Step 5  Now we use the where() method to filter the required information based on the predicate provided by the user.

Step 6  In this step, we display the details fetched. And end the program.

Example

Let us discuss this problem with an example.

using System.Linq;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
class Student{

   // Three variables to store roll number, class, and name of the student
   int roll;
   int std;
   string name;
   // Creating the string method which returns roll number, name, and standard of student
   public override string ToString(){
      return roll + " " + name + " " + std;
   }
   static void Main(string[] args){

      // Declaring a list variable
      List<Student> student = new List<Student>(){

         // Details of students of the chess team
         new Student{ roll = 21, name = "Ankit", std = 10 },
         new Student{ roll = 12, name = "Abhinay", std = 10 },
         new Student{ roll = 07, name = "Shubham", std = 11 },
         new Student{ roll = 14, name = "Shreyank", std = 12 },
         new Student{ roll = 10, name = "Shanawaz", std = 11 }

      };
      
      // Using the Where() function we search through the student details
      IEnumerable<Student> Query = student.Where(s => s.name[0] == 'S');

      // Displaying the student details
      Console.WriteLine("Roll Name Standard");
      Console.WriteLine("- - - - - - - - - - - - - - - - - ");
      foreach (Student e in Query) {

         // Call the to string method
         Console.WriteLine(e.ToString());
      }
   }
} 

Output

Roll Name Standard
- - - - - - - - - - - - - - - - -
07 Shubham 11
14 Shreyank 12
10 Shanawaz 11 

Time Complexity

The time complexity of the where() method is constant which in Big-O notation can be called O(1). Thus the time complexity of the whole code is O(1).

Conclusion

In this article, we have discussed a C# Program to Find the List of Students whose Name Starts with 'S' using the where() Method of List Collection using LINQ. We understood the where() method. Then we understood the algorithm and finally, we learned the code. Then we understood the time complexity of the code.

We hope this article has been helpful in enhancing your knowledge about C#.

Updated on: 31-Mar-2023

237 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements