本文介绍了如何对列表进行排序<T>通过对象中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Order 的类,它具有 OrderIdOrderDateQuantity 和 .我有这个 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();

这篇关于如何对列表进行排序&lt;T&gt;通过对象中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 16:45