Dada una lista de empleados, ahora ordenamos la lista según el salario en orden descendente y cuyo departamento es XYZ. Así que usamos el método OrderByDescending() de LINQ junto con el método Where() . OrderByDescending() se usa para ordenar la lista especificada en orden descendente. Para resolver el problema dado, usamos la siguiente consulta LINQ:
var result_set = Geeks.Where(emp=>emp.Emp_Department==”XYZ”).OrderByDescending(sal => sal.Emp_Salary);
Aquí, el método Where() encuentra aquellos empleados cuyo departamento es «XYZ» y el método OrderByDescending() los clasifica en orden descendente según su salario.
Ejemplo:
Input: {id = 202, Name = Mohit, Salary = 10000, Department = XYZ} {id = 204, Name = Sumit, Salary = 20000, Department = ABC} {id = 205, Name = Pritam, Salary = 80000, Department = ABC} {id = 206, Name = Poonam, Salary = 30000, Department = XYZ} Output: {id = 206, Name = Poonam, Salary = 30000, Department = XYZ} {id = 202, Name = Mohit, Salary = 10000, Department = XYZ}
C#
// C# program to sort a list of employees based on // salary in descending order and whose department is XYZ 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) { // List to store the details of employees List<Geek> Geeks = new List<Geek>() { new Geek{emp_id = 101, Emp_Name = "Amit", Emp_Salary = 50000,Emp_Department = "XYZ"}, new Geek{emp_id = 102, Emp_Name = "Poonam", Emp_Salary = 65000,Emp_Department = "ABC"}, new Geek{emp_id = 103, Emp_Name = "Priya", Emp_Salary = 45000,Emp_Department = "ABC"}, new Geek{emp_id = 104, Emp_Name = "Sita", Emp_Salary = 20000,Emp_Department = "XYZ"}, new Geek{emp_id = 105, Emp_Name = "kiran", Emp_Salary = 70000,Emp_Department = "ABC"}, new Geek{emp_id = 106, Emp_Name = "Sohan", Emp_Salary = 40000,Emp_Department = "XYZ"}, }; // Using the where command we have selected the // geeks having XYZ department and then we have // sorted the data in descending order using // OrderByDescending() command var result_set = Geeks.Where(emp => emp.Emp_Department == "XYZ").OrderByDescending( sal => sal.Emp_Salary); // Display the results foreach (Geek emp in result_set) { Console.WriteLine(emp.emp_id + " " + emp.Emp_Name + " " + emp.Emp_Salary + " " + emp.Emp_Department); } } }
Producción:
101 Amit 50000 XYZ 106 Sohan 40000 XYZ 104 Sita 20000 XYZ
Publicación traducida automáticamente
Artículo escrito por raghu135ram y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA