c# - 如何订购匿名类型的 IEnumerable<T>?

标签 c# .net-3.5 anonymous-types

请看下面的代码,我不知道为什么我的订单不起作用,有什么想法吗?

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

最佳答案

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

OrderBy()返回一个排序的集合,它不修改 ordersList。

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

关于c# - 如何订购匿名类型的 IEnumerable<T>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/823430/

相关文章:

c# - 什么是最高效的,为什么?

c# - SQL Server 2008 + .NET3.5 + LINQ 中出现 "Server failed to resume the transaction"错误

visual-studio-2008 - 无法在自动属性 setter 上设置断点?为什么?

c# - 将 LINQ 结果传递给函数

c# - 当鼠标在控件上方时如何消除 ToolStripDropDownButton 上的蓝色焦点

c# - 如何在 ASP.NET ApiController 中捕获 g-recaptcha-response?

c# - Ninject 覆盖绑定(bind)

c# - 在 Opserver 中正确配置仪表板

c++ - 匿名聚合规则

.net - 使用 GROUP BY 和 Count(*) 将 LINQ 查询为匿名类型