Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering linq query with secondary sort [duplicate]

Tags:

c#

linq

Possible Duplicate:
Multiple “order by” in LINQ

I have a list of orders and I need to order it by the date of the order and then a secondary sort by the price of the order. I'm not sure how exactly to do this since I tried orders.OrderBy(o => o.Date).OrderBy(o => o.Price), but it doesn't work. Any help is much appreciated. Thank you

like image 527
Shane Avatar asked Aug 03 '12 15:08

Shane


3 Answers

You want to use the ThenBy function:

orders.OrderBy(o => o.Date).ThenBy(o => o.Price)

like image 76
FAtBalloon Avatar answered Oct 24 '22 08:10

FAtBalloon


The other option is to use the LINQ query syntax instead of the method syntax:

 List<Order> sortedOrders = from o in orders
 orderby o.Date, O.Price
 select o;
like image 23
scott.korin Avatar answered Oct 24 '22 09:10

scott.korin


You can use:

orders.OrderBy(o => o.Date).ThenBy(o => o.Price)

like image 26
Am1rr3zA Avatar answered Oct 24 '22 08:10

Am1rr3zA