Home >Backend Development >C#.Net Tutorial >C# program uses LINQ to sort a list of employees in department ABC based on salary
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.
To sort a list of employees based on their salary and department using LINQ, you can follow the steps below −
public class Employee { public string Name { get; set; } public int Salary { get; set; } public string Department { get; set; } }
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" } };
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}"); }The Chinese translation of
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.
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}"); } } }
John: 50000 Bob: 40000
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!