在C#中使用Orderby降序对元素进行降序排序。
以下是我们的列表-
IList<Employee> emp = new List<Employee>() { new Employee() { EmployeeRank = 3, EmpName = "Tom", EmpMarks = 90 } , new Employee() { EmployeeRank = 4, EmpName = "Katie", EmpMarks = 95 } };
现在要根据学生姓名对列表进行排序,请使用Orderby。默认值为升序,因此对于降序,请使用降序。
var res = from str in emp orderby str.EmpName descending select str;
这是完整的代码。
using System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { IList<Employee> emp = new List<Employee>() { new Employee() { EmployeeRank = 4, EmpName = "Amit", EmpMarks = 90 } , new Employee() { EmployeeRank = 05, EmpName = "Raman", EmpMarks = 95 } }; var res = from str in emp orderby str.EmpName descending select str; Console.WriteLine("Student List (Descending Order):"); foreach (var list in res) Console.WriteLine(list.EmpName); } } public class Employee { public int EmployeeRank { get; set; } public string EmpName { get; set; } public int EmpMarks { get; set; } }
输出结果
Student List (Descending Order): Raman Amit