在LINQ中,排序运算符用于根据一个或多个属性以升序或降序重新排列给定的序列。 LINQ提供了5种不同类型的排序运算符:
- 按订单
- OrderByDescending
- 然后
- 然后降序
- 撤销
OrderBy运算符
OrderBy运算符用于按升序重新排列给定序列的元素。默认情况下,此运算符将升序转换给定序列的顺序。无需在查询表达式中添加额外的升序条件,这意味着升序关键字是可选的。您还可以使用降序关键字以降序更改给定序列的顺序。
查询语法中的OrderBy : OrderBy运算符支持C#和VB.Net语言的查询语法。在查询语法中,使用orderby单词,如以下示例所示:
例子:
// C# program to print the employee
// name in 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;
}
}
public 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
// employee in ascending order
// Using orderby clause in Query Syntax
var res = from e in emp
orderby e.emp_name
select e.emp_name;
foreach(var val in res)
{
Console.WriteLine("Employee Name: {0}", val);
}
}
}
输出:
Employee Name: Anil
Employee Name: Anjita
Employee Name: Anju
Employee Name: Rohit
Employee Name: Soniya
Employee Name: Supriya
方法语法中的OrderBy :方法语法中的OrderBy运算符在两种不同的类型中被重载:
- OrderBy
(IEnumerable 此方法根据键以升序对给定序列的项目进行排序。,Func ): - OrderBy
(IEnumerable 此方法通过使用指定的比较器以升序对给定序列的项目进行排序。,Func ,IComparer ):
它同时存在于Queryable和Enumerable类中。并支持C#和VB.NET语言的方法语法。如下例所示:
例子:
// C# program to print the salary of
// the employees in ascending
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;
}
}
public 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 = 80000},
new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
emp_hire_date = "12/1/2016", emp_salary = 60000},
new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female",
emp_hire_date = "17/6/2015", emp_salary = 50000},
};
// Print the salary of the employees
// in ascending order using the
// OrderBy operator
var res = emp.OrderBy(a => a.emp_salary);
Console.WriteLine("Salary of the employees: ");
foreach(var val in res)
{
Console.WriteLine(val.emp_salary);
}
}
}
输出:
Salary of the employees:
20000
30000
40000
50000
60000
80000
多重排序
在LINQ中,允许在单个查询中对集合的多个字段进行排序,并且每个字段都用逗号分隔。当您对集合执行多次排序时,首先根据第一个条件对集合进行排序,然后如果给定集合中的两个字段相似,则它将对第二个字段进行排序,依此类推。如下例所示,emp_salary包含两个相似的值,因此将根据emp_id进行排序。
例子:
// C# program to illustrate multiple sorting
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;
}
}
public 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 = 20000},
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 = 80000},
new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
emp_hire_date = "12/1/2016", emp_salary = 60000},
new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female",
emp_hire_date = "17/6/2015", emp_salary = 50000},
};
// Perform multiple sorting
var res = from e in emp
orderby e.emp_salary, e.emp_id
select e;
foreach(var val in res)
{
Console.WriteLine("Employee Salary: {0} Employee Id: {1}",
val.emp_salary, val.emp_id);
}
}
}
输出:
Employee Salary: 20000 Employee Id: 209
Employee Salary: 20000 Employee Id: 210
Employee Salary: 40000 Employee Id: 211
Employee Salary: 50000 Employee Id: 214
Employee Salary: 60000 Employee Id: 213
Employee Salary: 80000 Employee Id: 212