Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C# Program to Print the Employees Whose Name Started with ‘S’ and Age is Greater than 23 Using LINQ

  • Last Updated : 01 Nov, 2021

LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives the ability to .NET languages to generate queries to retrieve data from the data source. It removes the mismatch between programming languages and databases and the syntax used to create a query is the same no matter which type of data source is used. In this article, we will learn how to print the details of the employees whose name starts with ‘S’ and their age is greater than 23 using LINQ. 

Example:

Input : List of Employees:
         {{id = 101, name = "Sravan", age = 32},
          {id = 102, name = "deepu",  age = 15},
          {id = 103, name = "manoja", age = 13},
          {id = 104, name = "Sathwik", age = 12},
          {id = 105, name = "Saran",  age = 25}}
Output : {{id = 105, name = "sravan", age = 32},
          {id = 105, name = "Saran",  age = 25}}
 
Input : List of Employees:
        {{id = 102, name = "deepu",  age = 15},
         {id = 103, name = "manoja", age = 13}}
Output : No Output

Approach:

To find the list of employees whose name starts with ‘S’ and their age is greater than 23 follow the following steps:

  1. Create a list of employees with four variables(Id, name, department, and age).
  2. Iterate through the employee details by using where clause and get the employee details by choosing employee  name starts with’ S’ and their age is greater than 23. So, we write where emp.Name[0] == ‘S’ && emp.Age > 23 .
  3. Now call the ToString() method.
  4. Display the employee  details.

Example:

C#




// C# program to display the details of 
// those employees whose name is starts 
// with S and their age is greater than 23
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
class Employee{
      
// Declare four variables - id, age, department, and name
int id; 
int age;
string department;
string name;
  
// Get the to string method that returns 
// id, age, department, and name
public override string ToString()
{
    return id + " " + name + " " + age + " " + department;
}
  
// Driver code
static void Main(string[] args)
{
      
    // Declare a list variable 
    List<Employee> emp = new List<Employee>()
    {
          
        // Create 5 Employee details
        new Employee{ id = 101, name = "Sravan",  
                      age = 32, department = "HR" },
        new Employee{ id = 102, name = "deepu"
                      age = 15, department = "Development" },
        new Employee{ id = 103, name = "manoja"
                      age = 13, department = "Development" },
        new Employee{ id = 104, name = "Sathwik"
                      age = 12, department = "HR" },
        new Employee{ id = 105, name = "Saran"
                      age = 25, department = "Designing" }
    };
      
    // Iterate the Employee by selecting Employee 
    // name starts with S and age is greater than 23
    IEnumerable<Employee> result = from e in emp 
                                   where e.name[0] == 'S' && e.age > 23 
                                   select e;
      
    // Display employee details
    Console.WriteLine("ID  Name  Age Department");
    Console.WriteLine("+++++++++++++++++++++++++");
    foreach (Employee x in result)
    {
          
        // Call the to string method
        Console.WriteLine(x.ToString());
    }    
}
}

Output:

ID  Name  Age Department
+++++++++++++++++++++++++
101 Sravan 32 HR
105 Saran 25 Designing


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!