- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to Sort a List of Employees Based on Salary and Whose Department is ABC 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 and department.
Sorting a List of Employees Based on Salary and Department using LINQ
To sort a list of employees based on their salary and department using LINQ, you can follow the steps below ā
1. 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; } }
2. Create a List of Employees
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" } };
3. Use LINQ to Sort the List of Employees by Salary and Department
var sortedEmployees = employees .Where(e => e.Department == "ABC") .OrderByDescending(e => e.Salary) .ThenBy(e => e.Name);
4. 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
Step 1 - We define a class called Employee to represent an employee. The class has three properties: 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 list of employees based on their salary and department. We first filter the list to include only employees whose department is "ABC". 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.
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 = "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}"); } } }
Output
John: 50000 Bob: 40000
Conclusion
Sorting a list of employees based on their salary 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 and department.