Home >Backend Development >C#.Net Tutorial >C# program to sort a list of employees whose department is XYZ based on salary in descending order using LINQ

C# program to sort a list of employees whose department is XYZ based on salary in descending order using LINQ

王林
王林forward
2023-09-22 16:45:081150browse

C# 程序使用 LINQ 根据工资降序对员工列表进行排序,其部门是 XYZ

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

Use LINQ to sort a list of employees whose department is XYZ in descending order by salary -

To sort a list of employees based on salary in descending order and department using LINQ, you can follow these steps -

  • Create a class to represent employees -

public class Employee {
   public string Name { get; set; }
   public int Salary { get; set; }
   public string Department { get; set; }
}
  • Create employee list -

List 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 = "XYZ" },
   new Employee { Name = "Alice", Salary = 70000, Department = "XYZ" }
};
  • Sort a list of employees by salary and department in descending order using LINQ -

var sortedEmployees = employees
   .Where(e => e.Department == "XYZ")
   .OrderByDescending(e => e.Salary)
   .ThenBy(e => e.Name);
  • Loop through the sorted list and print out the name and salary of each employee -

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

illustrate

In step 1, we define a class named Employee to represent employees. This class has three properties: Name, Salary, and Department.

In step 2, we create a list of employees and initialize it with some sample data.

In step 3, we use LINQ to sort the list of employees in descending order based on their salary and department. We first filter the list to only include employees with department "XYZ". We then sort the filtered list in descending order based on the employee's salary, and then in ascending order based on their name. The result is a sorted list of employees that match the filter criteria.

In step 4, we iterate over the sorted list of employees and print out each employee's name and salary using string interpolation.

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 = "XYZ" },
         new Employee { Name = "Alice", Salary = 70000, Department = "XYZ" }
      };
   
      var sortedEmployees = employees
         .Where(e => e.Department == "XYZ")
         .OrderByDescending(e => e.Salary)
         .ThenBy(e => e.Name);
   
      foreach (var employee in sortedEmployees) {
         Console.WriteLine($"{employee.Name}: {employee.Salary}");
      }
   }
}

Output

Alice: 70000
Bob: 40000

in conclusion

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

The above is the detailed content of C# program to sort a list of employees whose department is XYZ based on salary in descending order using LINQ. 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