Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to order a IEnumerable<T> of anonymous type?

See the code below, I don't know why my ordering is not working, any ideas?

var orderSample = new { ProductName = "", Qty = 0, UserFullName = "" };
var ordersList = (new[] { orderSample }).ToList();

//loop thru another collection and fill ordersList by adding an order at a time
ordersList.Add(new { ProductName = "Product1", Qty = 5, UserFullName = "Mr. Smith" });

//sort the orders by name - DOESN'T WORK
ordersList.OrderBy(p => p.ProductName);

gvReport3.DataSource = ordersList;
gvReport3.DataBind();
like image 983
roman m Avatar asked May 05 '09 05:05

roman m


1 Answers

var sortedList = ordersList.OrderBy(p => p.ProductName).ToList();

OrderBy() returns a sorted collection, it does not modify the ordersList.

If you need to modify the ordersList, use Sort instead.

like image 125
Amy B Avatar answered Sep 20 '22 00:09

Amy B