Dada una lista de empleados, ahora ordenamos la lista de empleados según su salario usando LINQ. Entonces, para esta tarea, usamos el método OrderBy() . Este método se utiliza para clasificar los elementos de la secuencia especificada en orden ascendente.
Ejemplo:
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}
Acercarse:
1. Cree una lista de empleados junto con su identificación, nombre, salario y departamento.
2. Ahora ordene la lista de empleados según su salario utilizando el método OrderBy().
var result_set = Geeks.OrderBy(sal => sal.Emp_Salary);
O también podemos ordenar la lista usando la cláusula order OrderBy de LINQ
var result_set = from emp in Geeks orderby emp.Emp_Salary select emp;
3. Muestre la lista ordenada usando el bucle foreach.
Ejemplo 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); } } }
104 Ram 20000 DEF 103 krishna 45000 ABC 101 arjun 50000 ABC 106 karna 50000 ABC 102 bheem 65000 DEF 105 kiran 70000 DEF
Ejemplo 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); } } }
Producción
204 Sonam 20000 DEF 203 Mohit 45000 ABC 201 Sumit 50000 ABC 202 Rohan 65000 DEF 205 Shive 70000 DEF
Publicación traducida automáticamente
Artículo escrito por pulamolusaimohan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA