📜  LINQ |分类运算符|然后降序

📅  最后修改于: 2021-05-29 17:32:44             🧑  作者: Mango

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

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

然后按降序运算符

ThenByDescending运算符用于按降序实现二级排序。 ThenByDescending运算符支持多重排序。通常,ThenByDescending方法与OrderBy方法一起使用。 OrderBy()方法首先按降序对序列或集合中的元素进行排序,然后使用ThenByDescending()方法再次对OrderBy()方法的结果进行降序排序。或者换句话说,在LINQ中,集合是根据OrderBy方法给出的主字段的第一个排序,之后,主要排序的结果再次由ThenByDescending方法给出的辅助字段进行排序。

  • 它不支持C#和VB.Net语言的查询语法。
  • 它支持C#和VB.Net语言的方法语法。
  • 它同时存在于Queryable和Enumerable类中。
  • 查询语法中的辅助排序使用逗号分隔。
  • 它是通过使用延迟执行来实现的。

范例1:

// C# program to print the employees
// name and their salary
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 and
        // salary of the employees
        // Using OrderByDescending and
        // ThenByDescending method
        var res = emp.OrderByDescending(e => 
           e.emp_name).ThenByDescending(e =>
                              e.emp_salary);
  
        foreach(var val in res)
        {
            Console.WriteLine("Employee Name: {0}  Salary: {1}",
                                    val.emp_name, val.emp_salary);
        }
    }
}
输出:
Employee Name: Supriya  Salary: 40000
Employee Name: Soniya  Salary: 30000
Employee Name: Rohit  Salary: 40000
Employee Name: Anju  Salary: 50000
Employee Name: Anjita  Salary: 20000
Employee Name: Anil  Salary: 40000

范例2:

// C# program to print the employees
// IDs and their salary
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 IDs and
        // salary of the employees
        // Using OrderBy and ThenByDescending 
        // method
        var res = emp.OrderBy(e => 
            e.emp_id).ThenByDescending(e => 
                              e.emp_salary);
  
        foreach(var val in res)
        {
            Console.WriteLine("Employee ID: {0} Salary: {1}"
                              , val.emp_id, val.emp_salary);
        }
    }
}
输出:
Employee ID: 209 Salary: 20000
Employee ID: 210 Salary: 30000
Employee ID: 211 Salary: 40000
Employee ID: 212 Salary: 40000
Employee ID: 213 Salary: 40000
Employee ID: 214 Salary: 50000