C# - Sort employees list based on salary in descending order using LINQ

Learn, how to sort a list of employees based on salary in descending order and whose department is XYZ using Linq in C#? By Nidhi Last updated : April 01, 2023

Here, we will create a list of employees and then sort a list of employees using the OrderByDescending() method based on salary in descending order and then check the department of employees using the Linq Where() method.

C# program to sort employees list based on salary (in descending order) and department using LINQ

The source code to sort a list of employees based on salary in descending order and whose department is XYZ using Linq is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to sort the list of employees based on 
//salary in descending order whose department is XYZ

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

public class Employee {
  int ID;
  string Name;
  int Salary;
  string Department;

  public override string ToString() {
    return ID + " " + Name + " " + Salary + " " + Department;
  }

  static void Main(string[] args) {
    List <Employee> employees = new List <Employee> () {
      new Employee {ID = 101, Name = "Amit  ", Salary = 4000, Department = "ABC"},
      new Employee {ID = 102, Name = "Gautam", Salary = 6000, Department = "XYZ"},
      new Employee {ID = 103, Name = "Salman", Salary = 3000, Department = "ABC"},
      new Employee {ID = 104, Name = "Ram   ", Salary = 2000, Department = "XYZ"},
      new Employee {ID = 105, Name = "Shyam ", Salary = 7000, Department = "ABC"},
      new Employee {ID = 106, Name = "Kishor", Salary = 5000, Department = "XYZ"}
    };

    var result = employees.Where(emp => emp.Department == "XYZ").OrderByDescending(sal => sal.Salary);

    Console.WriteLine("ID  Name  Salary  Department");
    Console.WriteLine("============================");
    foreach(Employee emp in result) {
      Console.WriteLine(emp.ToString());
    }
    Console.WriteLine("============================");
  }
}

Output

ID  Name  Salary  Department
============================
102 Gautam 6000 XYZ
106 Kishor 5000 XYZ
104 Ram    2000 XYZ
============================
Press any key to continue . . .

Explanation

In the above program, we created a class Demo that contains the Main() method. In the Main() method we created a list of employees with fields ID, Name, Salary, and Department.

var result = employees.Where(emp => emp.Department == "XYZ").OrderByDescending(sal => sal.Salary);

In the above code, we sorted a list of employees based on salary using OrderByDescending() in descending order and checked the department using Where() method.

Console.WriteLine("ID  Name  Salary  Department");
Console.WriteLine("============================");
foreach (Employee emp in result)
{
    Console.WriteLine(emp.ToString());
}
Console.WriteLine("============================");

In the above code, we accessed the employee detail one by one and print on the console screen.

C# LINQ Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.