LINQ中排序操作符(七)

Linq中的排序操作符包括OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse,提供了升序或者降序排序

一、OrderBy操作符

OrderBy操作符用于对输入序列中的元素进行排序,排序基于一个委托方法的返回值顺序。排序过程完成后,会返回一个类型为IOrderEnumerable<T>的集合对象。其中IOrderEnumerable<T>接口继承自IEnumerable<T>接口

Sample Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderOperation
{
    public class Products
    {
        public int Id { get; set; }
        public int CategoryId { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
        public DateTime CreateTime { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // 初始化数据
            List<Products> listProduct = new List<Products>()
            {
               new Products(){Id=1, CategoryId=1, Name="C#高级编程第10版", Price=100.67, CreateTime=DateTime.Now},
               new Products(){Id=2, CategoryId=1, Name="Redis开发和运维", Price=69.9, CreateTime=DateTime.Now.AddDays(-19)},
               new Products(){Id=3, CategoryId=1, Name="ASP.NET Core", Price=57, CreateTime=DateTime.Now.AddMonths(-3)},
               new Products(){Id=4, CategoryId=1, Name="Entity Framework 6.x", Price=97, CreateTime=DateTime.Now.AddMonths(-1)}
            };
            Console.WriteLine("方法语法");
            // 1、查询方法,返回匿名类
            var list = listProduct.OrderBy(p => p.CreateTime).Select(p => new { id = p.Id, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime }).ToList();
            foreach (var item in list)
            {
                Console.WriteLine("item:{0}", item);
            }
            Console.WriteLine("查询表达式");
            // 2、查询表达式,返回匿名类
            var listExpress = from p in listProduct orderby p.CreateTime select new { id = p.Id, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime };
            foreach (var item in listExpress)
            {
                Console.WriteLine("item:{0}", item);
            }

            Console.ReadKey();
        }
    }
}
View Code

二、OrderByDescending操作符

OrderByDescending操作符的功能与OrderBy操作符基本相同,二者只是排序的方式不同。OrderBy是升序排序,而OrderByDescending则是降序排列

Sample Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderOperation
{
    public class Products
    {
        public int Id { get; set; }
        public int CategoryId { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
        public DateTime CreateTime { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // 初始化数据
            List<Products> listProduct = new List<Products>()
            {
               new Products(){Id=1, CategoryId=1, Name="C#高级编程第10版", Price=100.67, CreateTime=DateTime.Now},
               new Products(){Id=2, CategoryId=1, Name="Redis开发和运维", Price=69.9, CreateTime=DateTime.Now.AddDays(-19)},
               new Products(){Id=3, CategoryId=1, Name="ASP.NET Core", Price=57, CreateTime=DateTime.Now.AddMonths(-3)},
               new Products(){Id=4, CategoryId=1, Name="Entity Framework 6.x", Price=97, CreateTime=DateTime.Now.AddMonths(-1)}
            };
            Console.WriteLine("方法语法");
            // 1、查询方法,返回匿名类
            var list = listProduct.OrderByDescending(p => p.CreateTime).Select(p => new { id = p.Id, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime }).ToList();
            foreach (var item in list)
            {
                Console.WriteLine("item:{0}", item);
            }
            Console.WriteLine("查询表达式");
            // 2、查询表达式,返回匿名类
            var listExpress = from p in listProduct orderby p.CreateTime descending select new { id = p.Id, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime };
            foreach (var item in listExpress)
            {
                Console.WriteLine("item:{0}", item);
            }

            Console.ReadKey();
        }
    }
}
View Code

三、ThenBy操作符

ThenBy操作符可以对一个类型为IOrderedEnumerable<T>,(OrderBy和OrderByDesceding操作符的返回值类型)的序列再次按照特定的条件顺序排序。ThenBy操作符实现按照次关键字对序列进行升序排列

Sample Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderOperation
{
    public class Products
    {
        public int Id { get; set; }
        public int CategoryId { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
        public DateTime CreateTime { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // 初始化数据
            List<Products> listProduct = new List<Products>()
            {
               new Products(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
               new Products(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
               new Products(){Id=3,CategoryId=2, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
               new Products(){Id=4,CategoryId=3, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)}
            };

            // 注意:ThenBy()的方法语法和查询表达式写法有些不同。
            Console.WriteLine("方法语法升序排序");
            // 1、查询方法,按照商品分类升序排序,如果商品分类相同在按照价格升序排序 返回匿名类
            var list = listProduct.OrderBy(p => p.CategoryId)
                                  .ThenBy(p => p.Price)
                                  .Select(p => new { id = p.CategoryId, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime }).ToList();
            foreach (var item in list)
            {
                Console.WriteLine("item:{0}", item);
            }
            Console.WriteLine("查询表达式升序排序");
            var listExpress = from p in listProduct
                              orderby p.CategoryId, p.Price
                              select new { id = p.CategoryId, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime };
            foreach (var item in listExpress)
            {
                Console.WriteLine("item:{0}", item);
            }

            Console.WriteLine("方法语法降序排序");
            // 1、查询方法,按照商品分类降序排序,如果商品分类相同在按照价格升序排序 返回匿名类
            var listDesc = listProduct.OrderByDescending(p => p.CategoryId)
                                      .ThenBy(p => p.Price)
                                      .Select(p => new { id = p.CategoryId, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime }).ToList();
            foreach (var item in listDesc)
            {
                Console.WriteLine("item:{0}", item);
            }
            Console.WriteLine("查询表达式降序排序");
            var listExpressDesc = from p in listProduct orderby p.CategoryId descending, p.Price select new { id = p.CategoryId, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime };
            foreach (var item in listExpressDesc)
            {
                Console.WriteLine("item:{0}", item);
            }
            Console.ReadKey();
        }
    }
}
View Code

四、ThenByDescending操作符

ThenByDescending操作符于ThenBy操作符非常类似,只是是按照降序排序,实现按照次关键字对序列进行降序排列

Sample Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderOperation
{
    public class Products
    {
        public int Id { get; set; }
        public int CategoryId { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
        public DateTime CreateTime { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // 初始化数据
            List<Products> listProduct = new List<Products>()
            {
               new Products(){Id=1, CategoryId=1, Name="C#高级编程第10版", Price=100.67, CreateTime=DateTime.Now},
               new Products(){Id=2, CategoryId=1, Name="Redis开发和运维", Price=69.9, CreateTime=DateTime.Now.AddDays(-19)},
               new Products(){Id=3, CategoryId=2, Name="活着", Price=57, CreateTime=DateTime.Now.AddMonths(-3)},
               new Products(){Id=4, CategoryId=3, Name="高等数学", Price=97, CreateTime=DateTime.Now.AddMonths(-1)}
            };
            // 注意:ThenByDescending()的方法语法和查询表达式写法有些不同。
            Console.WriteLine("方法语法升序排序");
            // 1、查询方法,按照商品分类升序排序,如果商品分类相同在按照价格降序排序 返回匿名类
            var list = listProduct.OrderBy(p => p.CategoryId).ThenByDescending(p => p.Price).Select(p => new { id = p.CategoryId, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime }).ToList();
            foreach (var item in list)
            {
                Console.WriteLine("item:{0}", item);
            }
            Console.WriteLine("查询表达式升序排序");
            var listExpress = from p in listProduct orderby p.CategoryId, p.Price descending select new { id = p.CategoryId, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime };
            foreach (var item in listExpress)
            {
                Console.WriteLine("item:{0}", item);
            }

            Console.WriteLine("方法语法降序排序");
            // 1、查询方法,按照商品分类降序排序,如果商品分类相同在按照价格降序排序 返回匿名类
            var listDesc = listProduct.OrderByDescending(p => p.CategoryId).ThenByDescending(p => p.Price).Select(p => new { id = p.CategoryId, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime }).ToList();
            foreach (var item in listDesc)
            {
                Console.WriteLine("item:{0}", item);
            }
            Console.WriteLine("查询表达式降序排序");
            var listExpressDesc = from p in listProduct orderby p.CategoryId descending, p.Price descending select new { id = p.CategoryId, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime };
            foreach (var item in listExpressDesc)
            {
                Console.WriteLine("item:{0}", item);
            }
            Console.ReadKey();
        }
    }
}
View Code

posted @ 2020-04-14 10:35  LuckyZLi  阅读(223)  评论(0编辑  收藏  举报