Mệnh đề orderby ... descending trong query syntax (cú pháp truy vấn) sắp xếp chuỗi theo thứ tự giảm dần.
Ví dụ minh họa
Danh sách nhân viên:
IList<Employee> emp = new List<Employee>() {
new Employee() { EmployeeRank = 4, EmpName = "Amit", EmpMarks = 90 },
new Employee() { EmployeeRank = 5, EmpName = "Raman", EmpMarks = 95 }
};
Sắp xếp giảm dần theo tên:
var res = from str in emp
orderby str.EmpName descending
select str;
Mã nguồn hoàn chỉnh
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 = 5, EmpName = "Raman", EmpMarks = 95 }
};
var res = from str in emp
orderby str.EmpName descending
select str;
Console.WriteLine("Danh sách sinh viên (Giảm dần):");
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; }
}
Kết quả đầu ra
Danh sách sinh viên (Giảm dần): Raman Amit
Cú pháp phương thức (Method Syntax) tương đương: emp.OrderByDescending(e => e.EmpName)