ADVERTISEMENT
ADVERTISEMENT

C# program to find the list of students whose name contains 4 characters using where() method of List collection using Linq

Here, we are going to learn how to find the list of students whose name contains 4 characters using where() method of List collection using Linq?
Submitted by Nidhi, on August 28, 2020

Here we will find the list of students whose name contains 4 characters. Here we will use the where() method. In the where() we will specify the condition to select student names contains 4 characters. To use where() method we need to import "System.Linq" and "System.Collections.Generic" namespaces.

Program:

The source code to find the list of students whose name contains 4 characters, is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to find a list of students whose name contains 
//4 characters 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.Length==4);

        Console.WriteLine("Student Names:");
        foreach (string name in result)
        {
            Console.WriteLine(name);
        }
    }
}

Output:

Student Names:
Amit
Ayan
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.Length==4);

In the above code, where() method is used to select student according to a specified condition. Here we find the students whose name contains 4 characters.

Console.WriteLine("Student Names:");
foreach (string name in result)
{
    Console.WriteLine(name);
}

Here we printed the select student name using "foreach" on the console screen.

C# LINQ Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.