How to Sort a Listlt;Tgt; by a property in the object(如何对列表进行排序lt;Tgt;通过对象中的属性)
问题描述
我有一个名为 Order
的类,它具有 OrderId
、OrderDate
、Quantity
和 <代码>总计代码>.我有这个 Order
类的列表:
I have a class called Order
which has properties such as OrderId
, OrderDate
, Quantity
, and Total
. I have a list of this Order
class:
List<Order> objListOrder = new List<Order>();
GetOrderList(objListOrder); // fill list of orders
现在我想根据 Order
对象的一个属性对列表进行排序,例如我需要按订单日期或订单 ID 对其进行排序.
Now I want to sort the list based on one property of the Order
object, for example I need to sort it by the order date or order id.
如何在 C# 中做到这一点?
How can i do this in C#?
推荐答案
我能想到的最简单的方法就是使用Linq:
The easiest way I can think of is to use Linq:
List<Order> SortedList = objListOrder.OrderBy(o=>o.OrderDate).ToList();
这篇关于如何对列表进行排序<T>通过对象中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何对列表进行排序<T>通过对象中的属性


- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 输入按键事件处理程序 2022-01-01