2009-05-05 67 views
5

看到下面的代碼,我不知道爲什麼我的訂購不起作用,有什麼想法?如何訂購匿名類型的IEnumerable <T>?

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

回答

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

OrderBy()返回一個有序集合,它不修改ordersList。

如果您需要修改ordersList,請改用Sort。

+0

謝謝,作品很棒! – 2009-05-05 05:30:28