Come ordinare un IEnumerable < > di tipo anonimo?
-
05-07-2019 - |
Domanda
Vedi il codice qui sotto, non so perché il mio ordine non funziona, qualche idea?
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();
Soluzione
var sortedList = ordersList.OrderBy(p => p.ProductName).ToList();
OrderBy () restituisce una raccolta ordinata, non modifica la lista degli ordini.
Se è necessario modificare la lista ordini, utilizzare Ordina invece.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow