C# Program to Sort a List of Employees Based on Salary using LINQ
Last Updated :
06 Dec, 2021
Given a list of employees, now we sort the list of employees according to their salary using LINQ. So to this task, we use the OrderBy() method. This method is used to sort the elements of the specified sequence in ascending order.
Example:
Input:
{id = 101, Name = Rohit, Salary = 50000, Department = HR}
{id = 104, Name = Rohit, Salary = 10000, Department = Development}
{id = 106, Name = Rohit, Salary = 80000, Department = HR}
{id = 108, Name = Rohit, Salary = 20000, Department = Development}
Output:
{id = 104, Name = Rohit, Salary = 10000, Department = Development}
{id = 108, Name = Rohit, Salary = 20000, Department = Development}
{id = 101, Name = Rohit, Salary = 50000, Department = HR}
{id = 106, Name = Rohit, Salary = 80000, Department = HR}
Approach:
1. Create a list of employees along with their id, name, salary, and department.
2. Now sort the employee's list according to their salary using the OrderBy() method.
var result_set = Geeks.OrderBy(sal => sal.Emp_Salary);
Or we can also sort the list using the order OrderBy clause of LINQ
var result_set = from emp in Geeks orderby emp.Emp_Salary select emp;
3. Display the sorted list using foreach loop.
Example 1:
C#
// C# program to sort a list of employees
// based on salary. Using OrderBy() method
using System;
using System.Linq;
using System.Collections.Generic;
class Geek{
int emp_id;
string Emp_Name;
int Emp_Salary;
string Emp_Department;
static void Main(string[] args)
{
// Geeks data
List<Geek> Geeks = new List<Geek>()
{
new Geek{emp_id = 101, Emp_Name = "arjun",
Emp_Salary = 50000, Emp_Department = "ABC"},
new Geek{emp_id = 102, Emp_Name = "bheem",
Emp_Salary = 65000, Emp_Department = "DEF"},
new Geek{emp_id = 103, Emp_Name = "krishna",
Emp_Salary = 45000, Emp_Department = "ABC"},
new Geek{emp_id = 104, Emp_Name = "Ram",
Emp_Salary = 20000, Emp_Department = "DEF"},
new Geek{emp_id = 105, Emp_Name = "kiran",
Emp_Salary = 70000, Emp_Department = "DEF"},
new Geek{emp_id = 106, Emp_Name = "karna",
Emp_Salary = 50000, Emp_Department = "ABC"},
};
// We have sorted the data using OrderBy() command
var result_set = Geeks.OrderBy(sal => sal.Emp_Salary);
// Display the sorted result
foreach (Geek emp in result_set)
{
Console.WriteLine(emp.emp_id + " " +
emp.Emp_Name + " " +
emp.Emp_Salary + " " +
emp.Emp_Department);
}
}
}
Output104 Ram 20000 DEF
103 krishna 45000 ABC
101 arjun 50000 ABC
106 karna 50000 ABC
102 bheem 65000 DEF
105 kiran 70000 DEF
Example 2:
C#
// C# program to sort a list of employees
// based on salary. Using OrderBy() method
using System;
using System.Linq;
using System.Collections.Generic;
class Geek{
int emp_id;
string Emp_Name;
int Emp_Salary;
string Emp_Department;
static void Main(string[] args)
{
// Geeks data
List<Geek> Geeks = new List<Geek>()
{
new Geek{emp_id = 201, Emp_Name = "Sumit",
Emp_Salary = 50000, Emp_Department = "ABC"},
new Geek{emp_id = 202, Emp_Name = "Rohan",
Emp_Salary = 65000, Emp_Department = "DEF"},
new Geek{emp_id = 203, Emp_Name = "Mohit",
Emp_Salary = 45000, Emp_Department = "ABC"},
new Geek{emp_id = 204, Emp_Name = "Sonam",
Emp_Salary = 20000, Emp_Department = "DEF"},
new Geek{emp_id = 205, Emp_Name = "Shive",
Emp_Salary = 70000, Emp_Department = "DEF"},
};
// We have sorted the data using OrderBy linq clause
var result_set = from emp in Geeks orderby emp.Emp_Salary select emp;
// Display the sorted result
foreach (Geek emp in result_set)
{
Console.WriteLine(emp.emp_id + " " +
emp.Emp_Name + " " +
emp.Emp_Salary + " " +
emp.Emp_Department);
}
}
}
Output
204 Sonam 20000 DEF
203 Mohit 45000 ABC
201 Sumit 50000 ABC
202 Rohan 65000 DEF
205 Shive 70000 DEF
Similar Reads
C# Program to Sort a List of Employees Based on Salary and Whose Department is ABC using LINQ Given a list of employees, now our task is to sort the given list of employees based on salary and whose department is ABC using LINQ. Example: Input: {id = 101, name = "Sumit", salary = 10000, department = ABC} {id = 102, name = "Rohit", salary = 20000, department = HR} {id = 103, name = "Mohit", s
2 min read
C# Program to Sort a List of Employees Based on Salary in Descending Order and Whose Department is XYZ using LINQ Given a list of employees, now we sort the list according to the salary in descending order and whose department is XYZ. So we use the OrderByDescending() method of LINQ along with the Where() method. The OrderByDescending() is used to sort the specified list in descending order. To solve the given
2 min read
C# Program to Check the Salary of all Employees is Less than 10000 using LINQ Given the data of employees, now our task is to check if all the employee salaries are less than 10000. So we use the All() method of LINQ. This method is used to check if all the elements in the source sequence satisfy the given condition. It will return true if all the elements present in the give
3 min read
C# Program to Sort a List of Integers Using the LINQ OrderBy() Method Given a list of integers, now our task is to sort the given list of integers. So we use the OrderBy() method of LINQ. This method is used to sort the elements in the collection in ascending order. This method is overloaded in two different ways: OrderBy<TSource, TKey>(IEnumerable<TSource
2 min read
C# Program to Sort a List of String Names Using the LINQ OrderBy() Method Given a list of string names, now our task is to sort the names given in the list using the OrderBy() method of LINQ. This method is used to sort the elements in the collection in ascending order. While using this method in the LINQ query you don't need to add an extra condition to sort the list in
2 min read
C# Program to Join Employee and Department Class using LINQ Join Query Given two classes named as Employee and Department, now we join Employee and Department class with the help of LINQ join Query. So to this task, we use the Join clause. This clause is used to join two data sources into one source which has some common attributes. It always takes two data sources and
3 min read