Dada una lista de empleados, ahora nuestra tarea es ordenar la lista dada de empleados según el salario y cuyo departamento es ABC usando LINQ.
Ejemplo:
Input: {id = 101, name = "Sumit", salary = 10000, department = ABC} {id = 102, name = "Rohit", salary = 20000, department = HR} {id = 103, name = "Mohit", salary = 30000, department = ABC} {id = 104, name = "Sunil", salary = 40000, department = ABC} Output: {id = 101, name = "Sumit", salary = 10000, department = ABC} {id = 103, name = "Mohit", salary = 30000, department = ABC} {id = 104, name = "Sunil", salary = 40000, department = ABC}
Acercarse:
1. Cree una lista de empleados que contenga la identificación, el nombre, el salario y el departamento
2. Usar el método OrderBy() y el método Where() para ordenar la lista de empleados según el salario y cuyo departamento es ABC
var result_set = Geeks.Where(emp => emp.Emp_Department == "ABC").OrderBy( sal => sal.Emp_Salary);
3. Mostrar la lista ordenada
Ejemplo:
C#
// C# program to display a sorted list of employees based // on Salary and whose Department is ABC using System; using System.Linq; using System.Collections.Generic; public class Geek{ int emp_id; string Emp_Name; int Emp_Salary; string Emp_Department; // Driver code 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"}, }; // Using the where command we have selected the geeks // having ABC department and then we have sorted the // data using OrderBy() command var result_set = Geeks.Where(emp => emp.Emp_Department == "ABC").OrderBy( sal => sal.Emp_Salary); foreach (Geek emp in result_set) { Console.WriteLine(emp.emp_id + " " + emp.Emp_Name +" " + emp.Emp_Salary + " " + emp.Emp_Department); } } }
Producción
103 krishna 45000 ABC 101 arjun 50000 ABC 106 karna 50000 ABC
Publicación traducida automáticamente
Artículo escrito por pulamolusaimohan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA