Home  >  Article  >  Backend Development  >  C# program to find a list of students whose names begin with "S" using the where() method of list collection using LINQ

C# program to find a list of students whose names begin with "S" using the where() method of list collection using LINQ

WBOY
WBOYforward
2023-09-11 21:29:021385browse

C# 程序使用使用 LINQ 的列表收集的 where() 方法查找姓名以“S”开头的学生列表

Introduction

The topic to be learned in this article is to use the where() method of the List collection and LINQ to write a C# program to find the list of students whose names start with 'S'.

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

Before we continue and understand the algorithm and code of the C# program, use the where() method of LINQ's List Collection to find the list of students whose names begin with 'S'. Let's briefly review the shorthand form of LINQ.

Language Integrated Query (LINQ)

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

The best feature of LINQ is that it provides a single way to get data from many sources, including databases and XML documents. With LINQ, users can write code that is easier to understand, simpler, and more beautiful. It also provides other features such as filtering, sorting, grouping data and even modifying data.

Now, it’s time to give some explanations about the where() method, which is also called the Enumerable.Where() method and belongs to the namespace System.Linq.

Where767cb18821eb42d0be2dbae71b5cc905(IEnumerable51957562cc2184b000bf5b07c9632283,Funcff5fbc224e3c4313d042b3d11149c3f2) The Chinese translation of

is:

Where767cb18821eb42d0be2dbae71b5cc905(IEnumerable51957562cc2184b000bf5b07c9632283,Funcff5fbc224e3c4313d042b3d11149c3f2)

This method belongs to the namespace System.Linq. It filters a sequence of values ​​based on a predicate. Let's take a look at its definition syntax -

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

This method has a type parameter TSource, which tells us the type of the source element. There are also two parameters, the source is an IEnumerable8742468051c85b06f0a0af9e3e506b5c used for filtering, and the predicate is a function used to test whether each element meets the conditions.

This is a return method that returns a collection of items in the input sequence that meets the conditions, expressed as an IEnumerable8742468051c85b06f0a0af9e3e506b5c. ArgumentNullException may be thrown when the predicate or source is null.

We will take a problem statement that will make it easy for us to understand the conceptual approach of where().

In a school, there is a chess team composed of 5 members from different classes. They are Ankit, Abhinay, Shubham, Shreyank and Shahnawaz. They study in different classes. A blazer with the initial 'S' is required. Therefore, in order to know the names of students starting with 'S', the principal has tasked you to write a program that selects the names of students starting with 'S'.

Now, in the next section, we will discuss the algorithm to write a C# program to find the list of students whose names start with 'S' using where() method of LINQ list collection.

algorithm

The following are the steps you need to follow when writing program code using the where() method.

Step 1 - Remember to declare the correct namespace using the where() method, i.e. System.Linq.

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

Step 3 In the next step, we create a string method that returns the roll number, name, and criteria of the student.

Step 4 In the main() section, 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 obtained detailed information. Then end the program.

Example

Let us discuss this issue through 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 can be called O(1) in Big-O notation. So the time complexity of the entire code is O(1).

in conclusion

In this article, we discussed a C# program that uses the where() method of LINQ's list collection to find a list of students whose names begin with "S". We understand the where() method. Then we learned about algorithms and finally we learned about code. Then we understand the time complexity of the code.

We hope this article has been helpful in enhancing your understanding of C#.

The above is the detailed content of C# program to find a list of students whose names begin with "S" using the where() method of list collection using LINQ. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete