2009-05-05 6 views
5

Vedere il codice qui sotto, non so perché il mio ordine non funziona, qualche idea?Come ordinare un oggetto IEnumerable <T> di tipo anonimo?

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(); 

risposta

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

OrderBy() restituisce una raccolta differenziata, non modifica l'OrdersList.

Se è necessario modificare l'elenco ordini, utilizzare Ordina.

+0

grazie, funziona alla grande !!! –