C# program to print the employees whose name started with character 'S' using LINQ

Here, we are going to learn how to print the employees whose name started with character 'S' using LINQ in C#?
Submitted by Nidhi, on August 21, 2020

Here we will create an Employee class that contains data members ID, Name, Age, and Salary. Here we created List collection for employees and search only those employees whose name started with character 'S' using LINQ and print them on the console screen.

Program:

The source code to print employee details using LINQ in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

// Program to print the employees whose name 
//started with character 'S' using LINQ in C#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Employee
{
    int     ID      ;
    string  Name    ;
    int     Age     ;
    int     Salary  ;


    public override string ToString()
    {
        return ID + " " + Name+" "+Age+" "+Salary;
    }

    static void Main(string[] args)
    {
        List<Employee> employees = new List<Employee>()
        {
             new Employee {ID=101,   Name="Sumit"    ,Age=23, Salary=4000},
             new Employee {ID=102,   Name="Kiran"    ,Age=24, Salary=6000},
             new Employee {ID=103,   Name="Suman"    ,Age=25, Salary=7000},
             new Employee {ID=104,   Name="Raman"    ,Age=26, Salary=9000},
        };

        IEnumerable<Employee> Query =
            from emp in employees
            where emp.Name[0]=='S'
            select emp;


        Console.WriteLine("ID  Name  Age Salary");
        Console.WriteLine("=====================");
        foreach (Employee s in Query)
        {
            Console.WriteLine(s.ToString());
        }
        Console.WriteLine("=====================");
    }
}

Output:

ID  Name  Age Salary
=====================
101 Sumit 23 4000
103 Suman 25 7000
=====================
Press any key to continue . . .

Explanation:

In the above program, we created an Employee class that contains data members ID, Name, Age, and Salary. Here we override the ToString() method to return Employee information. The Employee class also one more method is known as Main().

List<Employee> employees = new List<Employee>()
{
    new Employee {ID=101,   Name="Sumit"    ,Age=23, Salary=4000},
    new Employee {ID=102,   Name="Kiran"    ,Age=24, Salary=6000},
    new Employee {ID=103,   Name="Suman"    ,Age=25, Salary=7000},
    new Employee {ID=104,   Name="Raman"    ,Age=26, Salary=9000},
};

In the Main() method, we created List collection for Employees, It contains information of 4  employees.

IEnumerable<Employee> Query =
    from emp in employees
    where emp.Name[0]=='S'
    select emp;

In the above code, we created a query to get a list of employees whose names started with the character 'S'. Then we printed the Employee detail using the "foreach" loop on the console screen.

C# LINQ Programs »


ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




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.