排序操作符,包括OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse,提供了升序或者降序排序。
OrderBy操作符将序列中的元素按照升序排列。
注意:orderby必须在select之前出现,查询表达式最后只可能出现select或者groupby。
student类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 排序操作符 8 { 9 public class Student 10 { 11 12 //姓名 13 public string Name { get; set; } 14 //成绩 15 public int Score { get; set; } 16 public int Order { get; set; } 17 //构造函数 18 public Student(string name, int score, int order) 19 { 20 this.Name = name; 21 this.Score = score; 22 this.Order = order; 23 } 24 } 25 }
teacher类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 排序操作符 8 { 9 /// <summary> 10 /// Teacher类 11 /// </summary> 12 public class Teacher 13 { 14 //姓名 15 public string Name { get; set; } 16 //学生集合 17 public List<Student> Students { get; set; } 18 19 public Teacher(string name, List<Student> students) 20 { 21 this.Name = name; 22 this.Students = students; 23 } 24 } 25 }
Program:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 排序操作符 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //使用集合初始化器初始化Teacher集合 14 List<Teacher> teachers = new List<Teacher> { 15 new Teacher("徐老师", 16 new List<Student>(){ 17 new Student("宋江",80,1), 18 new Student("卢俊义",95,2), 19 new Student("朱武",45,3) 20 } 21 ), 22 new Teacher("姜老师", 23 new List<Student>(){ 24 new Student("林冲",90,2), 25 new Student("花荣",85,4), 26 new Student("柴进",58,5) 27 } 28 ), 29 new Teacher("樊老师", 30 new List<Student>(){ 31 new Student("关胜",100,6), 32 new Student("阮小七",70,7), 33 new Student("时迁",30,0) 34 } 35 ) 36 }; 37 38 #region OrderBy 39 //OrderBy 表达式 40 var queryOrderBy = from t in teachers 41 from s in t.Students 42 where s.Score < 60 43 orderby s.Order 44 select s; 45 //方法 46 var query = teachers.SelectMany(p => p.Students).Where(s => s.Score < 60).OrderBy(s => s.Order).Select(s => s); 47 foreach (var item in queryOrderBy) 48 { 49 Console.WriteLine("姓名:"+item.Name+",分数:"+item.Score); 50 } 51 foreach (var item in query) 52 { 53 Console.WriteLine("姓名:" + item.Name + ",分数:" + item.Score); 54 } 55 #endregion 56 57 Console.ReadKey(); 58 } 59 } 60 }
运行结果:
二、OrderByDescending
OrderByDescending操作符将序列中的元素按照降序排列。
示例:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 排序操作符 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //使用集合初始化器初始化Teacher集合 14 List<Teacher> teachers = new List<Teacher> { 15 new Teacher("徐老师", 16 new List<Student>(){ 17 new Student("宋江",80,1), 18 new Student("卢俊义",95,2), 19 new Student("朱武",45,3) 20 } 21 ), 22 new Teacher("姜老师", 23 new List<Student>(){ 24 new Student("林冲",90,2), 25 new Student("花荣",85,4), 26 new Student("柴进",58,5) 27 } 28 ), 29 new Teacher("樊老师", 30 new List<Student>(){ 31 new Student("关胜",100,6), 32 new Student("阮小七",70,7), 33 new Student("时迁",30,0) 34 } 35 ) 36 }; 37 38 #region OrderByDescending 39 //表达式 40 var queryDesc = from t in teachers 41 from s in t.Students 42 where s.Score < 60 43 orderby s.Order descending 44 select s; 45 //方法 46 var query1 = teachers.SelectMany(p => p.Students).Where(s => s.Score < 60).OrderByDescending(s => s.Order).Select(s => s); 47 foreach (var item in queryDesc) 48 { 49 Console.WriteLine("姓名:" + item.Name + ",分数:" + item.Score); 50 } 51 foreach (var item in queryDesc) 52 { 53 Console.WriteLine("姓名:" + item.Name + ",分数:" + item.Score); 54 } 55 #endregion 56 57 Console.ReadKey(); 58 } 59 } 60 }
运行效果:
三、ThenBy排序
ThenBy操作符实现按照次关键字对序列进行升序排列。此操作符的查询语法和方法语法略有不同,例如:
//查询语法
var query=from e in Employees orderby e.FirstName,e.LastName select e;
//方法语法
var q=Employees.OrderBy(e=>e.FirstName).ThenBy(e=>e.LastName).Select(e=>e);
示例:
Category类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ThenBy 8 { 9 public class Category 10 { 11 public Guid CategoryId { get; set; } 12 public string CategoryName { get; set; } 13 public int Order { get; set; } 14 } 15 }
Products类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ThenBy 8 { 9 public class Products 10 { 11 public Guid Id { get; set; } 12 public Guid CategoryId { get; set; } 13 public string Name { get; set; } 14 public int Order { get; set; } 15 public DateTime CreateTime { get; set; } 16 } 17 }
Program:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ThenBy 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 List<Products> pro = new List<Products>() { 14 new Products(){Id=Guid.NewGuid(),Name="格林童话",CategoryId=Guid.NewGuid(),Order=1,CreateTime=DateTime.Now.AddHours(2)}, 15 new Products(){Id=Guid.NewGuid(),Name="美女与野兽",CategoryId=Guid.NewGuid(),Order=1,CreateTime=DateTime.Now}, 16 new Products(){Id=Guid.NewGuid(),Name="世界如此险恶,您的内心需要强大",CategoryId=Guid.NewGuid(),Order=0,CreateTime=DateTime.Now.AddHours(3)} 17 }; 18 //查询语法 19 var query = (from p in pro orderby p.Order, p.CreateTime select p); 20 //方法 21 var q = pro.OrderBy(p => p.Order).ThenBy(p => p.CreateTime).Select(p => p); 22 foreach (var item in query) 23 { 24 Console.WriteLine("name:"+item.Name+"id:"+item.Id+",order:"+item.Order+",CreateTime:"+item.CreateTime); 25 } 26 foreach (var item in q) 27 { 28 Console.WriteLine("name:" + item.Name + "id:" + item.Id + ",order:" + item.Order + ",CreateTime:" + item.CreateTime); 29 } 30 31 Console.ReadKey(); 32 } 33 } 34 }
运行效果:
四、ThenByDescending
ThenByDescending操作符实现按照次关键字对序列进行降序排列。此操作符的查询语法与方法语法略有不同,例如:
//查询语法
var query=from e in Employees orderby e.FirstName,e.LastName descending select e;
//方法语法
var q=Employees.OrderBy(e=>e.FirstName).ThenByDescending(e=>e.LastName).Select(e=>e);
示例:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ThenBy 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 List<Products> pro = new List<Products>() { 14 new Products(){Id=Guid.NewGuid(),Name="格林童话",CategoryId=Guid.NewGuid(),Order=1,CreateTime=DateTime.Now.AddHours(2)}, 15 new Products(){Id=Guid.NewGuid(),Name="美女与野兽",CategoryId=Guid.NewGuid(),Order=1,CreateTime=DateTime.Now}, 16 new Products(){Id=Guid.NewGuid(),Name="世界如此险恶,您的内心需要强大",CategoryId=Guid.NewGuid(),Order=0,CreateTime=DateTime.Now.AddHours(3)} 17 }; 18 //查询语法 19 var query = from p in pro orderby p.Order, p.CreateTime descending select p; 20 //方法 21 var q = pro.OrderBy(p => p.Order).ThenByDescending(p => p.CreateTime); 22 foreach (var item in query) 23 { 24 Console.WriteLine("name:" + item.Name + "id:" + item.Id + ",order:" + item.Order + ",CreateTime:" + item.CreateTime); 25 } 26 foreach (var item in q) 27 { 28 Console.WriteLine("name:" + item.Name + "id:" + item.Id + ",order:" + item.Order + ",CreateTime:" + item.CreateTime); 29 } 30 31 Console.ReadKey(); 32 } 33 } 34 }
运行效果:
五、Reverse
Reverse将会把序列中的元素按照从后到前的顺序反转。需要注意的是,Reverse方法的返回值是void,例如:
var q=Employees.Select(e=>e.FirstName).ToList();
q.Reverse();
示例:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ThenBy 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string[] str = { "A", "B", "C", "D", "E"}; 14 var query = str.Select(p => p).ToList(); 15 query.Reverse(); 16 foreach (var item in query) 17 { 18 Console.WriteLine(item); 19 } 20 21 Console.ReadKey(); 22 } 23 } 24 }
运行效果: