在LINQ中,排序运算符用于根据一个或多个属性以升序或降序重新排列给定的序列。 LINQ提供了5种不同类型的排序运算符:
- 按订单
- OrderByDescending
- 然后
- 然后降序
- 撤销
然后按运算符
thenBy运算符用于按升序实现二级排序。 ThenBy运算符支持多重排序。通常,ThenBy方法与OrderBy方法一起使用。 OrderBy()方法,首先将序列或集合中的元素按升序排序,然后使用ThenBy()方法再次对OrderBy()方法的结果进行升序排序。或者换句话说,在LINQ中,集合是根据OrderBy方法给出的主字段的第一个排序,之后,主要排序的结果再次由ThenBy方法给出的辅助字段进行排序。
- 它不支持C#和VB.Net语言的查询语法。
- 它支持C#和VB.Net语言的方法语法。
- 它同时存在于Queryable和Enumerable类中。
- 查询语法中的辅助排序使用逗号分隔,如示例1所示。
- 它是通过使用延迟执行来实现的。
范例1:
// C# program to print the employees
// names and their gender in the
// ascending 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 and
// gender of the employees in
// ascending order In query syntax
var res = from e in emp
orderby e.emp_name, e.emp_gender
select e;
foreach(var val in res)
{
Console.WriteLine("Employee Name: {0} Gender: {1}",
val.emp_name, val.emp_gender);
}
}
}
输出:
Employee Name: Anil Gender: Male
Employee Name: Anjita Gender: Female
Employee Name: Anju Gender: Female
Employee Name: Rohit Gender: Male
Employee Name: Soniya Gender: Female
Employee Name: Supriya Gender: Female
范例2:
// C# program to print the employees
// names and their salary in the
// ascending 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 and
// salary of the employees in
// ascending order Using OrderBy
// and ThenBy method
var res = emp.OrderBy(e => e.emp_name).ThenBy(e => e.emp_salary);
foreach(var val in res)
{
Console.WriteLine("Employee Name: {0} Salary: {1}",
val.emp_name, val.emp_salary);
}
}
}
输出:
Employee Name: Anil Salary: 40000
Employee Name: Anjita Salary: 20000
Employee Name: Anju Salary: 50000
Employee Name: Rohit Salary: 40000
Employee Name: Soniya Salary: 30000
Employee Name: Supriya Salary: 40000