C# Program to Print the Employees Whose ID is Greater Than 101 Using LINQ - GeeksforGeeks

18 Oct, 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 display the employee details whose ID greater than 101 using LINQ.
Example:
Input:
List of employees:
      {{emp_id = 101, emp_name = "bobby",  emp_age = 12},
       {emp_id = 102, emp_name = "deepu",  emp_age = 15},
       {emp_id = 103, emp_name = "manoja", emp_age = 13},
       {emp_id = 104, emp_name = "saroja", emp_age = 14},
       {emp_id = 105, emp_name = "sravan", emp_age = 15},
       {emp_id = 106, emp_name = "navya",  emp_age = 12},
       {emp_id = 107, emp_name = "majnu",  emp_age = 12},
       {emp_id = 108, emp_name = "vishnu", emp_age = 12}}
Output:
      {{emp_id = 102, emp_name = "deepu",  emp_age = 15},
       {emp_id = 103, emp_name = "manoja", emp_age = 13},
       {emp_id = 104, emp_name = "saroja", emp_age = 14},
       {emp_id = 105, emp_name = "sravan", emp_age = 15},
       {emp_id = 106, emp_name = "navya",  emp_age = 12},
       {emp_id = 107, emp_name = "majnu",  emp_age = 12},
       {emp_id = 108, emp_name = "vishnu", emp_age = 12}}
      
Input:
List of employees:
      {{emp_id = 101, emp_name = "bobby",  emp_age = 12},
       {emp_id = 102, emp_name = "deepu",  emp_age = 15},
       {emp_id = 103, emp_name = "manoja", emp_age = 13}}
Output:
      {{emp_id = 102, emp_name = "deepu",  emp_age = 15},
       {emp_id = 103, emp_name = "manoja", emp_age = 13}}
Approach:
To display the employee details whose ID greater than 101 follow the following approach:
Create a list of employees with three variables (Id, name and age)Iterate through the employee details by using where function and get the employee details by choosing employee id greater than 101Select the details which are greater than 101Call the ToString() methodDisplay the employee details
Create a list of employees with three variables (Id, name and age)
Iterate through the employee details by using where function and get the employee details by choosing employee id greater than 101
Select the details which are greater than 101
Call the ToString() method
Display the employee details
Example:
C#
// C# program to display the details of the employee// whose ID greater than 101 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;  public class Employee{  // Declare 3 variables - id,age and nameint emp_id; int emp_age;string emp_name;  // Get the to string method that returns// id , name and agepublic override string ToString(){    return emp_id + " " + emp_name + " " + emp_age;}  // Driver codestatic void Main(string[] args){          // Declare a list variable     List<Employee> emp1 = new List<Employee>()    {                  // Create 8 employee details        new Employee{ emp_id = 101, emp_name = "bobby", emp_age = 12},        new Employee{ emp_id = 102, emp_name = "deepu", emp_age = 15},        new Employee{ emp_id = 103, emp_name = "manoja", emp_age = 13},        new Employee{ emp_id = 104, emp_name = "saroja", emp_age = 14},        new Employee{ emp_id = 105, emp_name = "sravan", emp_age = 15},        new Employee{ emp_id = 106, emp_name = "navya", emp_age = 12},        new Employee{ emp_id = 107, emp_name = "majnu", emp_age = 12},        new Employee{ emp_id = 108, emp_name = "vishnu", emp_age = 12},    };          // Iterate the Employee by selecting Employee    // id greater than 101    // Using where clause    IEnumerable<Employee> Query =     from employee in emp1 where employee.emp_id > 101 select employee;         // Display employee details    Console.WriteLine("ID  Name  Age");    Console.WriteLine("+++++++++++++");    foreach (Employee e in Query)    {                  // Call the to string method        Console.WriteLine(e.ToString());    }}}
Output:
ID  Name  Age
+++++++++++++
102 deepu 15
103 manoja 13
104 saroja 14
105 sravan 15
106 navya 12
107 majnu 12
108 vishnu 12
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
Top 50 C# Interview Questions & Answers
C# | How to insert an element in an Array?
C# | Inheritance
C# | List Class
Partial Classes in C#
Convert String to Character Array in C#
Lambda Expressions in C#
Linked List Implementation in C#