How to Sort a List by a property in the object

前端 未结 20 1980
醉梦人生
醉梦人生 2020-11-21 08:25

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

20条回答
  •  被撕碎了的回忆
    2020-11-21 08:33

    Simplest way to order a list is to use OrderBy

     List objListOrder = 
        source.OrderBy(order => order.OrderDate).ToList();
    

    If you want to order by multiple columns like following SQL Query.

    ORDER BY OrderDate, OrderId
    

    To achieve this you can use ThenBy like following.

      List objListOrder = 
        source.OrderBy(order => order.OrderDate).ThenBy(order => order.OrderId).ToList();
    

提交回复
热议问题