Home  >  Article  >  Backend Development  >  C# program uses LINQ to sort a list of employees in department ABC based on salary

C# program uses LINQ to sort a list of employees in department ABC based on salary

WBOY
WBOYforward
2023-09-02 10:21:02768browse

C# 程序使用 LINQ 根据薪水对部门为 ABC 的员工列表进行排序

In C#, LINQ (Language Integrated Query) is a powerful tool that can easily sort, filter and manipulate data. In this article, we will demonstrate how to use LINQ to sort a list of employees based on their salary and department.

Use LINQ to sort employee list based on salary and department

To sort a list of employees based on their salary and department using LINQ, you can follow the steps below −

1. Create a Class to Represent an Employee

public class Employee {
   public string Name { get; set; }
   public int Salary { get; set; }
   public string Department { get; set; }
}

2. Create a List of Employees

List<employee> employees = new List {
   new Employee { Name = "John", Salary = 50000, Department = "ABC" },
   new Employee { Name = "Mary", Salary = 60000, Department = "DEF" },
   new Employee { Name = "Bob", Salary = 40000, Department = "ABC" },
   new Employee { Name = "Alice", Salary = 70000, Department = "XYZ" }
};

3. Use LINQ to Sort the List of Employees by Salary and Department

var sortedEmployees = employees
   .Where(e => e.Department == "ABC")
   .OrderByDescending(e => e.Salary)
   .ThenBy(e => e.Name);

4. Iterate through the Sorted List and Print out each Employee's Name and Salary

foreach (var employee in sortedEmployees) {
   Console.WriteLine($"{employee.Name}: {employee.Salary}");
}
The Chinese translation of

Explanation

is:

Explanation

Step 1 - We define a class named Employee to represent an employee. This class has three attributes: Name, Salary and Department.

Step 2 - We create a list of employees and initialize it with some sample data.

Step 3 - We use LINQ to sort the employee list by salary and department. We first filter out the employees whose department is "ABC", and then sort the filtered list by salary in descending order and name in ascending order. The result is a sorted list of employees that meet the filter criteria.

Step 4 - We loop through the sorted list of employees and use string interpolation to print out the name and salary of each employee.

Example

using System;
using System.Collections.Generic;
using System.Linq;

public class Employee {
   public string Name { get; set; }
   public int Salary { get; set; }
   public string Department { get; set; }
}

class Program {
   static void Main(string[] args) {
      List<Employee> employees = new List <Employee>{
         new Employee { Name = "John", Salary = 50000, Department = "ABC" },
         new Employee { Name = "Mary", Salary = 60000, Department = "DEF" },
         new Employee { Name = "Bob", Salary = 40000, Department = "ABC" },
         new Employee { Name = "Alice", Salary = 70000, Department = "XYZ" }
      };
   
      var sortedEmployees = employees
         .Where(e => e.Department == "ABC")
         .OrderByDescending(e => e.Salary)
         .ThenBy(e => e.Name);
   
      foreach (var employee in sortedEmployees) {
         Console.WriteLine($"{employee.Name}: {employee.Salary}");
      }
   }
}

Output

John: 50000
Bob: 40000

Conclusion

Using LINQ to sort a list of employees based on salary and department is a simple and efficient way to manipulate data in C#. By using LINQ, you can easily filter, sort, and manipulate large amounts of data using just a few lines of code. We hope this article helped you understand how to sort a list of employees based on salary and department using LINQ.

The above is the detailed content of C# program uses LINQ to sort a list of employees in department ABC based on salary. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete