The source code to find the list of students whose name starts with 'S', is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//C# program to find a list of students whose name starts
//with 'S' using Where() method of List collection using Linq.
using System;
using System.Collections.Generic;
using System.Linq;
class Demo
{
static void Main(string[] args)
{
List<string> Students = new List<string>();
Students.Add("Amit");
Students.Add("Sumit");
Students.Add("Ayan");
Students.Add("Shaurya");
Students.Add("Sanaya");
IEnumerable<string> result = Students.Where(stu=>stu[0]=='S');
Console.WriteLine("Student Names start with 'S':");
foreach (string name in result)
{
Console.WriteLine(name);
}
}
}
Output:
Student Names start with 'S':
Sumit
Shaurya
Sanaya
Press any key to continue . . .
Explanation:
In the above program, we created a list and then add student names to the using Add() method.
IEnumerable<string> result = Students.Where(stu=>stu[0]=='S');
In the above code, where() method is used to select student according to a specified condition. Here we find the students whose name starts with 'S'.
Console.WriteLine("Student Names start with 'S':");
foreach (string name in result)
{
Console.WriteLine(name);
}
Here we printed the select student name using the "foreach" on the console screen.
Program:
The source code to find the list of students whose name starts with 'S', is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
Output:
Explanation:
In the above program, we created a list and then add student names to the using Add() method.
In the above code, where() method is used to select student according to a specified condition. Here we find the students whose name starts with 'S'.
Here we printed the select student name using the "foreach" on the console screen.