📌  相关文章
📜  使用 LINQ 打印工资在 6000 到 8000 之间的员工的 C# 程序

📅  最后修改于: 2022-05-13 01:55:27.280000             🧑  作者: Mango

使用 LINQ 打印工资在 6000 到 8000 之间的员工的 C# 程序

LINQ 被称为语言集成查询,它是在 .NET 3.5 中引入的。它使.NET语言能够生成查询以从数据源中检索数据。它消除了编程语言和数据库之间的不匹配,并且无论使用哪种类型的数据源,用于创建查询的语法都是相同的。在本文中,我们将使用 LINQ 获取工资在 6000 到 8000 之间的员工详细信息。

例子:

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}}

方法:

例子:

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 salary
int emp_id; 
string emp_dept;
string emp_name;
int emp_salary;
  
// Get the to string method that returns id,
// name, age and salary
public override string ToString()
{
    return emp_id + " " + emp_name + " " + 
           emp_dept + " " + emp_salary;
}
  
// Driver code
static void Main(string[] args)
{
      
    // Declare a list variable 
    List emp = new List()
    {
          
        // 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 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());
    }
}
}


输出:

ID  Name  Department Salary
+++++++++++++++++++++++++++
102 deepu Development 7000
103 manoja HR 6700