如何排序列表&LT; T&GT;通过在对象中的属性 [英] How to Sort a List<T> by a property in the object
本文介绍了如何排序列表&LT; T&GT;通过在对象中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为订单
有诸如的OrderId
,订购日期<属性的类/ code>,
数量
和总
。我有这个订单
类的列表:
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
现在我想根据订单
对象的一个属性列表进行排序,比如我需要通过订单日期或订单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;通过在对象中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文