C# Program to Print the Employees Whose Salary is Between 6000 and 8000 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 get the employee details whose salary is between 6000 and 8000 using LINQ.
Example:
Input:
List of employees:
      {{emp_id = 101, emp_name = "bobby", emp_age = 12, emp_salary = 2000}}
Output:
No Output
      
Input:
List of employees:
     {{emp_id = 101, emp_name = "bobby",  emp_age = 12, emp_salary = 8900},
      {emp_id = 102, emp_name = "deepu",  emp_age = 15, emp_salary = 7000},
      {emp_id = 103, emp_name = "manoja", emp_age = 13, emp_salary = 6700}}
Output:
      {{emp_id = 102, emp_name = "deepu",  emp_age = 15, emp_salary = 7000},
       {emp_id = 103, emp_name = "manoja", emp_age = 13, emp_salary = 6700}}
Approach:
To display the employee details whose salary is between 6000 and 8000 follow the following approach:
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 salary between 6000 and 8000.Select the details which are between 6000 and 8000Call the ToString methodDisplay 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 salary between 6000 and 8000.
Select the details which are between 6000 and 8000
Call the ToString method
Display the employee details
Example:
C#
// C# program to display the details of the employee's// whose salary is between 6000 and 8000 // using LINQ.using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;  class Employee{      // Declare 4 variables - id,age, name and salaryint emp_id; string emp_dept;string emp_name;int emp_salary;  // Get the to string method that returns id,// name, age and salarypublic override string ToString(){    return emp_id + " " + emp_name + " " +            emp_dept + " " + emp_salary;}  // Driver codestatic void Main(string[] args){          // Declare a list variable     List<Employee> emp = new List<Employee>()    {                  // Create 3 employee details        new Employee{ emp_id = 101, emp_name = "bobby",                       emp_dept = "HR", emp_salary = 8900  },        new Employee{ emp_id = 102, emp_name = "deepu",                       emp_dept = "Development", emp_salary = 7000 },        new Employee{ emp_id = 103, emp_name = "manoja",                       emp_dept = "HR", emp_salary = 6700 }};                            // Iterate the Employee by selecting Employee id    // greater than 101 using where function    IEnumerable<Employee> Query =     from employee in emp where employee.emp_salary >= 6000 &&    employee.emp_salary <= 8000 select employee;      // Display employee details    Console.WriteLine("ID  Name  Department Salary");    Console.WriteLine("+++++++++++++++++++++++++++");    foreach (Employee e in Query)    {                  // Call the to string method        Console.WriteLine(e.ToString());    }}}
Output:
ID  Name  Department Salary
+++++++++++++++++++++++++++
102 deepu Development 7000
103 manoja HR 6700
CSharp LINQ
CSharp-programs
Picked
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Destructors in C#
Extension Method in C#
HashSet in C# with Examples
Top 50 C# Interview Questions & Answers
C# | How to insert an element in an Array?
Partial Classes in C#
C# | Inheritance
C# | List Class
Difference between Hashtable and Dictionary in C#
Lambda Expressions in C#