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.
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.
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 IEnumerable
This is a return method that returns a collection of items in the input sequence that meets the conditions, expressed as an IEnumerable
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.
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()); } } }
Roll Name Standard - - - - - - - - - - - - - - - - - 07 Shubham 11 14 Shreyank 12 10 Shanawaz 11
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 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!