C# program to print the employees whose name started with ‘S’ and Age is greater than 23 using LINQ

Sometimes we get frustrated if we do not get the information we need in some articles. But this article has all the information you need when you read C# program to print the employees whose name started with ‘S’ and Age is greater than 23 using LINQ. Read patiently and understand.

C# program to print the employees whose name started with 'S' and Age is greater than 23 using LINQ

C# program to print the employees whose name started with ‘S’ and Age is greater than 23 using LINQ

// Program to print the employees whose last character 
// of the name is 'n' 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="Ram"      ,Age=24, Salary=6000},
             new Employee {ID=103,   Name="Sohan"    ,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' && emp.Age>23 
            select emp;

        Console.WriteLine("ID  Name  Age Salary");
        Console.WriteLine("=====================");
        foreach (Employee s in Query)
        {
            Console.WriteLine(s.ToString());
        }
        Console.WriteLine("=====================");
    }
}
ID  Name  Age Salary
=====================
103 Sohan 25 7000
=====================
Press any key to continue . . .

Final Words

We hope the C# program to print the employees whose name started with ‘S’ and Age is greater than 23 using LINQ article helped you in some way. And let us know if you have any doubts. Thanks

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment