查询排序 - OrderBy() ThenBy() OrderByDescending() ThenByDescending()
string[] names= { "mark", "steve", "adam" };
上升:
查询语法
var sortedNames =
from name in names
orderby name
select name;
方法语法
var sortedNames = names.OrderBy(name => name);
sortedNames 按以下顺序包含名称:adam
,mark
,steve
降:
查询语法
var sortedNames =
from name in names
orderby name descending
select name;
方法语法
var sortedNames = names.OrderByDescending(name => name);
sortedNames 按以下顺序包含名称:steve
,mark
,adam
按几个领域排序
Person[] people =
{
new Person { FirstName = "Steve", LastName = "Collins", Age = 30},
new Person { FirstName = "Phil" , LastName = "Collins", Age = 28},
new Person { FirstName = "Adam" , LastName = "Ackerman", Age = 29},
new Person { FirstName = "Adam" , LastName = "Ackerman", Age = 15}
};
查询语法
var sortedPeople = from person in people
orderby person.LastName, person.FirstName, person.Age descending
select person;
方法语法
sortedPeople = people.OrderBy(person => person.LastName)
.ThenBy(person => person.FirstName)
.ThenByDescending(person => person.Age);
结果
1. Adam Ackerman 29
2. Adam Ackerman 15
3. Phil Collins 28
4. Steve Collins 30