LINQ排序操作符用法
作者:.NET开发菜鸟 发布时间:2023-10-26 02:19:10
Linq中的排序操作符包括OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse,提供了升序或者降序排序。
一、OrderBy操作符
OrderBy操作符用于对输入序列中的元素进行排序,排序基于一个委托方法的返回值顺序。排序过程完成后,会返回一个类型为IOrderEnumerable<T>的集合对象。其中IOrderEnumerable<T>接口继承自IEnumerable<T>接口。下面来看看OrderBy的定义:
从上面的截图中可以看出,OrderBy是一个扩展方法,只要实现了IEnumerable<T>接口的就可以使用OrderBy进行排序。OrderBy共有两个重载方法:第一个重载的参数是一个委托类型和一个实现了IComparer<T>接口的类型。第二个重载的参数是一个委托类型。看看下面的示例:
定义产品类:
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; }
}
}
在Main()方法里面调用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrderOperation
{
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:{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:{item}");
}
Console.ReadKey();
}
}
}
结果:
从截图中可以看出,集合按照CreateTime进行升序排序。
在来看看第一个重载方法的实现:
先定义PriceComparer类实现IComparer<T>接口,PriceComparer类定义如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrderOperation
{
public class PriceComparer : IComparer<double>
{
public int Compare(double x, double y)
{
if (x > y)
{
return 1; //表示x>y
}
else if (x < y)
{
return -1; //表示x<y
}
else
{
return 0; //表示x=y
}
}
}
}
在Main()方法里面调用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrderOperation
{
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.Price,new PriceComparer()).Select(p => new { id = p.Id, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime }).ToList();
foreach (var item in list)
{
Console.WriteLine($"item:{item}");
}
Console.ReadKey();
}
}
}
结果:
注意:orderby必须在select之前出现,查询表达式最后只可能出现select或者groupby。
二、OrderByDescending
OrderByDescending操作符的功能与OrderBy操作符基本相同,二者只是排序的方式不同。OrderBy是升序排序,而OrderByDescending则是降序排列。下面看看OrderByDescending的定义:
从方法定义中可以看出,OrderByDescending的方法重载和OrderBy的方法重载一致。来看下面的例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrderOperation
{
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)}
};
// 注意:OrderByDescending的方法语法和查询表达式写法有些不同。
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:{item}");
}
Console.WriteLine("查询表达式");
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 list)
{
Console.WriteLine($"item:{item}");
}
Console.ReadKey();
}
}
}
结果:
从截图中可以看出:输出结果按照时间降序排序。在来看看另外一个重载方法的调用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrderOperation
{
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.Price, new PriceComparer()).Select(p => new { id = p.Id, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime }).ToList();
foreach (var item in list)
{
Console.WriteLine($"item:{item}");
}
Console.ReadKey();
}
}
}
结果:
输出结果也是按照时间降序排序。
三、ThenBy排序
ThenBy操作符可以对一个类型为IOrderedEnumerable<T>,(OrderBy和OrderByDesceding操作符的返回值类型)的序列再次按照特定的条件顺序排序。ThenBy操作符实现按照次关键字对序列进行升序排列。下面来看看ThenBy的定义:
从截图中可以看出:ThenBy()方法扩展的是IOrderedEnumerable<T>,因此ThenBy操作符长常常跟在OrderBy和OrderByDesceding之后。看下面的示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrderOperation
{
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:{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:{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:{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:{item}");
}
Console.ReadKey();
}
}
}
结果:
四、ThenByDescending
ThenByDescending操作符于ThenBy操作符非常类似,只是是按照降序排序,实现按照次关键字对序列进行降序排列。来看看ThenByDescending的定义:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrderOperation
{
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:{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:{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:{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:{item}");
}
Console.ReadKey();
}
}
}
结果:
五、Reverse
Reverse操作符用于生成一个与输入序列中元素相同,但元素排列顺序相反的新序列。下面来看看Reverse()方法的定义:
public static IEnumerable<TSource> Reverse<TSource>(this IEnumerable<TSource> source)
从方法定义中可以看到,这个扩展方法,不需要输入参数,返回一个新集合。需要注意的是,Reverse方法的返回值是void。看下面的例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ThenBy
{
class Program
{
static void Main(string[] args)
{
string[] str = { "A", "B", "C", "D", "E"};
var query = str.Select(p => p).ToList();
query.Reverse();
foreach (var item in query)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
运行效果:
来源:https://www.cnblogs.com/dotnet261010/p/6850936.html
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- Maven工程pom定义jdk版本今天把之前做的项目导进eclipse,然后发现报错,一些类在1.6中不支持,需要将JDK版本设置为1.7,
- 前言:项目是使用Java swing开发,可实现基础数据维护用户登录、系统首页酒店信息管理、主要模块是开房管理、退房管理、房间信息管理、顾客
- 带你手把手,用 java swing实现抖音上的表白程序1.准备工作a.需要下载一个带着swing插件的eclipseb.需要配置好JDKc
- 一、多表联合分页查询1.多表联合查询结果集建议使用VO类,当然也可以使用resultMappackage com.cjhx.tzld.ent
- 本文实例讲述了java读取properties文件的方法。分享给大家供大家参考。具体实现方法如下:package com.test.demo
- 前言先简单介绍下我们的使用场景,线上5台Broker节点的kafka承接了所有binlog订阅的数据,用于Flink组件接收数据做数据中台的
- 微信公众号开发一般是针对企业和组织的,个人一般只能申请订阅号,并且调用的接口有限,下面我们就来简单的描述下接入公众号的步骤:1、首先你需要一
- 主要从以下十几个方面对Hibernate做总结,包括Hibernate的检索方式,Hibernate中对象的状态,Hibernate的3种检
- spring:1)开源框架2)IoC(控制反转),将类的创建和依赖关系写在配置文件里,由配置文件注入,实现了松耦合3)AOP 将安全,事务等
- 目录一、简述二、内容一、简述利用C# TcpClient在局域网内传输文件,可是文件发送到对面的时候却要重新命名文件的。那可不可以连着文件名
- IDE的下载和安装:首先,到visual studio官网下载vs2019的安装程序。学生、或个人开发者免费下载第一个community版本
- 设计算法,计算两给定基因序列的相似程度。人类基因由4种核苷酸,分别用字母ACTG表示。要求编写一个程序,按以下规则比较两个基因序列并确定它们
- 在客户机和服务器之间建立单一的双向连接,这就意味着客户只需要发送一个请求到服务端,那么服务端则会进行处理,处理好后则将其返回给客户端,客户端
- 协议做如下规定:规定数据协议:序列号 长度 状态字 数据长度 数据1 &n
- 一、一级缓存二级缓存的概念解释(1)一级缓存就是Session级别的缓存,一个Session做了一个查询操作,它会把这个操作的结果放在一级缓
- 这篇文章主要介绍了SpringCloud Feign参数问题及解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 一,需求来源在开发过程中,可能会要使用Win7 ,Win8 ,Win10等不同版本的系统去做兼容性调试,也有时候会去针对特别的显卡,无线网卡
- 【前言】面向资源的 Restful 风格的 api 接口本着简洁,资源,便于扩展,便于理解等等各项优势,在如今的系统服务中越来越受欢迎。.n
- 本文实例为大家分享了Unity实现俄罗斯方块第一部分,供大家参考,具体内容如下准备工作1、新建一个2D项目,新建成功以后设置相机的一些参数2
- 在Java SE中,对IO流与集合的操作在应用中比较重要。接下来,我以一个小型项目的形式,演示IO流、集合等知识点在实践中的运用。该项目名称