C# Program For Implementing IEnumerable Interface 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 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.
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
Similar Reads
C# Program to Implement Multiple Interfaces in the Same Class Like a class, Interface can have methods, properties, events, and indexers as its members. But interface will contain only the declaration of the members. The implementation of interfaceâs members will be given by the class that implements the interface implicitly or explicitly. C# allows that a sin
3 min read
C# Program to Implement an Interface in a Structure Structure is a value type and a collection of variables of different data types under a single unit. It is almost similar to a class because both are user-defined data types and both hold a bunch of different data types. We can create a structure by using struct keyword. A structure can also hold co
2 min read
C# Program to Demonstrate Interface Implementation with Multi-level Inheritance Multilevel Inheritance is the process of extending parent classes to child classes in a level. In this type of inheritance, a child class will inherit a parent class, and as well as the child class also act as the parent class to other class which is created. For example, three classes called P, Q,
3 min read
C# Program to Generate Odd Numbers in Parallel using LINQ 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 matt
2 min read
C# | Explicit Interface Implementation An Interface is a collection of loosely bound items that have a common functionality or attributes. Interfaces contain method signatures, properties, events etc. Interfaces are used so that one class or struct can implement multiple behaviors. C# doesn't support the concept of Multiple Inheritance b
4 min read