C# Program to Sort a List of Employees Based on Salary in Descending Order and Whose Department is XYZ using LINQ


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

Sorting a List of Employees Based on Salary in Descending Order and Whose Department is XYZ using LINQ −

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

  • 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; }
}
  • Create a list of employees −

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" }
};
  • Use LINQ to sort the list of employees by salary in descending order and department −

var sortedEmployees = employees
   .Where(e => e.Department == "XYZ")
   .OrderByDescending(e => e.Salary)
   .ThenBy(e => e.Name);
  • 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}");
}

Explanation

In step 1, we define a class called Employee to represent an employee. The 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 based on their salary in descending order and department. We first filter the list to include only employees whose department is "XYZ". Then we 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 meet the filter criteria.

In step 4, we iterate through 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

Conclusion

Sorting a list of employees based on their salary in descending order and department using LINQ 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 with just a few lines of code. We hope this article has helped you understand how to use LINQ to sort a list of employees based on salary in descending order and department.

Updated on: 04-May-2023

234 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements