C# Program For Implementing IEnumerable Interface Using LINQ - GeeksforGeeks

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 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:
Create a list of employees with four variables(Id, name, department, and salary).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’.Now call the ToString() method.Display the employee details.
Create a list of employees with four variables(Id, name, department, and salary).
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’.
Now call the ToString() method.
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 salaryint id; int salary;string name;string department;  // Get the to string method that returns // id, name, department, and salarypublic override string ToString(){    return id + " " + name + " " +        salary + " " + department;}  // Driver codestatic 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
CSharp LINQ
CSharp-programs
Picked
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Extension Method in C#
HashSet in C# with Examples
Partial Classes in C#
Top 50 C# Interview Questions & Answers
C# | How to insert an element in an Array?
C# | List Class
C# | Inheritance
Linked List Implementation in C#
Lambda Expressions in C#
Convert String to Character Array in C#