📜  LINQ |分类运算符| OrderByDescending

📅  最后修改于: 2021-05-29 23:07:36             🧑  作者: Mango

在LINQ中,排序运算符用于根据一个或多个属性以升序或降序重新排列给定的序列。 LINQ中提供5种不同类型的排序运算符:

  1. 按订单
  2. OrderByDescending
  3. 然后
  4. 然后降序
  5. 撤销

OrderByDescending运算子

OrderByDescending运算符用于按降序重新排列给定序列的元素。它不支持C#和VB.Net中的查询语法。它仅支持方法语法。如果要按查询语法的降序重新排列或排序给定序列或集合的元素,请使用降序关键字,如以下示例所示。
并且在方法语法中,使用OrderByDescending()方法对给定序列或集合的元素进行排序。 Queryable和Enumerable类中都存在此方法。 C#和VB.Net语言都支持该方法语法。如下例所示。 OrderByDescending方法根据单个属性对集合的元素进行排序,不允许您使用多个属性对集合进行排序。

范例1:

// C# program to print the employee
// ID in descending order
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_name
    {
        get;
        set;
    }
  
    public string emp_gender
    {
        get;
        set;
    }
  
    public string emp_hire_date
    {
        get;
        set;
    }
  
    public int emp_salary
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List emp = new List() {
  
            new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female",
                                       emp_hire_date = "12/3/2017", emp_salary = 20000},
  
            new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female",
                                    emp_hire_date = "22/4/2018", emp_salary = 30000},
  
            new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male",
                                  emp_hire_date = "3/5/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female",
                                      emp_hire_date = "4/8/2017", emp_salary = 40000},
  
            new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
                                emp_hire_date = "12/1/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female",
                                  emp_hire_date = "17/6/2015", emp_salary = 50000},
        };
  
        // Query to print the ID of the
        // employees in descending order
        // Using Descending keyword in 
        // query syntax
        var res = from e in emp
                  orderby e.emp_id descending
                  select e;
  
        foreach(var val in res)
        {
            Console.WriteLine("Employee ID: {0}", val.emp_id);
        }
    }
}

输出:

Employee ID: 214
Employee ID: 213
Employee ID: 212
Employee ID: 211
Employee ID: 210
Employee ID: 209

范例2:

// C# program to print the employee
// name in descending order
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_name
    {
        get;
        set;
    }
  
    public string emp_gender
    {
        get;
        set;
    }
  
    public string emp_hire_date
    {
        get;
        set;
    }
  
    public int emp_salary
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List emp = new List() {
  
            new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female",
                                    emp_hire_date = "12/3/2017", emp_salary = 20000},
  
            new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female",
                                    emp_hire_date = "22/4/2018", emp_salary = 30000},
  
            new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male",
                                  emp_hire_date = "3/5/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female",
                                      emp_hire_date = "4/8/2017", emp_salary = 40000},
  
            new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
                                emp_hire_date = "12/1/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female",
                                  emp_hire_date = "17/6/2015", emp_salary = 50000},
        };
  
        // Query to print the name of the
        // employees in descending order
        // Using OrderByDescending method
        var res = emp.OrderByDescending(e => e.emp_name);
  
        foreach(var val in res)
        {
            Console.WriteLine("Employee Name: {0}", val.emp_name);
        }
    }
}

输出:

Employee Name: Supriya
Employee Name: Soniya
Employee Name: Rohit
Employee Name: Anju
Employee Name: Anjita
Employee Name: Anil