Open In App

C# Program For Implementing IEnumerable Interface Using LINQ

Improve
Improve
Like Article
Like
Save
Share
Report

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 are going to implement an IEnumerable interface using LINQ. This interface is used to iterate over the collections like list, stack, queue, etc. Or we can say that this interface is the base interface for all non-generic collections that can be enumerated.

Syntax:

IEnumerable<TSource>

Using IEnumerable we will display employee data whose name starts with ‘D’.

Example:

Input : List of Employees:
        {{id = 201, name = "Druva", age = 12},
         {id = 202, name = "Deepu", age = 15},
         {id = 203, name = "Manoja", age = 13},
         {id = 204, name = "Sathwik", age = 12},
         {id = 205, name = "Suraj", age = 15}}
Output : {{id = 201, name = "Druva", age = 12},
          {id = 202, name = "Deepu", age = 15}}
 
Input : List of Employees:
        {{id = 301, name = "Sathwik", age = 12},
         {id = 302, name = "Saran", age = 15}}
Output : No Output

Approach:

To find the list of employees whose name starts with letter ‘D’ follow the following steps:

  1. Create a list of employees with four 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 ‘D’ using s => s.name[0] == ‘D’.
  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 "D"
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 = 201, name = "Druva"
                      salary = 12000, department = "HR" },
        new Employee{ id = 202, name = "Deepu"
                      salary = 15000, department = "Development" },
        new Employee{ id = 203, name = "Manoja"
                      salary = 13000, department = "HR" },
        new Employee{ id = 204, name = "Sathwik"
                      salary = 12000, department = "Designing" },
        new Employee{ id = 205, name = "Suraj"
                      salary = 15000, department = "Development" }
    };
      
    // Iterate the Employee by selecting Employee 
    // name starts with D
    // Using IEnumerable interface
    IEnumerable<Employee> result = emp.Where(x => x.name[0] == 'D');
      
    // 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
++++++++++++++++++++++++++++
201 Druva 12000 HR
202 Deepu 15000 Development


Last Updated : 01 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads