Skip to content
Related Articles

Related Articles

C# Program to Print the Employees Whose Name Started With Character ‘S’ Using LINQ

Improve Article
Save Article
Like Article
  • 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 employees whose name starts with letter ‘S’ using LINQ. So to do our task we use Where() method. This method filters the given sequence or array of values based on the predicate and to use this method you need to add System.Linq and System.Collections.Generic namespaces in your program.

Syntax:

Where<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>)

Example:

Input : List of Employees:
        {{id = 101, name = "Sravan", age = 12},
         {id = 102, name = "deepu",  age = 15},
         {id = 103, name = "manoja", age = 13},
         {id = 104, name = "Sathwik", age = 12},
         {id = 105, name = "Saran",  age = 15}}
Output : {{id = 105, name = "sravan", age = 15},
          {id = 104, name = "Sathwik",age = 12},
          {id = 105, name = "Saran",  age = 15}}
   
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 letter ‘S’ follow the following steps:

  1. Create a list of employees with three variables(Id, name, department, and salary).
  2. Iterate through the employee details by using Where() function and get the employee details by choosing employee whose name starts with’ S’ using s => s.name[0] == ‘S’.
  3. Now call the ToString() method.
  4. Display the employee details.

Example:

C#




// C# program to display the details of those 
// employees whose name starts with character "S"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
class Employee{
      
// Declare 4 variables - id, name, 
// department, and salary
int id; 
int salary;
string name;
string department;
  
// Get the to string method that returns 
// id, name, department, and salary
public override string ToString()
{
    return id + " " + name + " "
       salary + " " + 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"
                      salary = 12000, department = "HR" },
        new Employee{ id = 102, name = "deepu"
                      salary = 15000, department = "Development" },
        new Employee{ id = 103, name = "manoja"
                      salary = 13000, department = "HR" },
        new Employee{ id = 104, name = "Sathwik"
                      salary = 12000, department = "Designing" },
        new Employee{ id = 105, name = "Saran"
                      salary = 15000, department = "Development" }
    };
      
    // Iterate the Employee by selecting Employee 
    // name starts with S
    IEnumerable<Employee> result = emp.Where(x => x.name[0] == 'S');
      
    // Display employee details
    Console.WriteLine("ID  Name  Salary  Department");
    Console.WriteLine("++++++++++++++++++++++++++++");
    foreach (Employee e in result)
    {
          
        // Call the to string method
        Console.WriteLine(e.ToString());
    }    
}
}

Output:

ID  Name  Salary  Department
++++++++++++++++++++++++++++
101 Sravan 12000 HR
104 Sathwik 12000 Designing
105 Saran 15000 Development

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!