![]() |
VOOZH | about |
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:
Output:
ID Name Salary Department ++++++++++++++++++++++++++++ 201 Druva 12000 HR 202 Deepu 15000 Development