Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

c# - 如何对列表排序 <T> 通过对象中的属性(How to Sort a List<T> by a property in the object)

I have a class called Order which has properties such as OrderId , OrderDate , Quantity , and Total .

(我有一个名为Order的类,它具有诸如OrderIdOrderDateQuantityTotal属性。)

I have a list of this Order class:

(我有此Order类的列表:)

List<Order> objListOrder = new List<Order>();
GetOrderList(objListOrder); // fill list of orders

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.

(现在,我想基于Order对象的一个??属性对列表进行排序,例如,我需要按订单日期或订单ID对其进行排序。)

How can i do this in C#?

(我该如何在C#中做到这一点?)

  ask by Shyju translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

我能想到的最简单的方法是使用Linq:

List<Order> SortedList = objListOrder.OrderBy(o=>o.OrderDate).ToList();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...