我试图通过降序排序以下列表,但我似乎无法弄清楚它是如何完成的:
var cust = item.Profiles.Select(c => new
{
id = c.CustId,
Name = c.Name
}).ToList();
ViewBag.Customers = new MultiSelectList(cust, "id", "Name");
这是我已经尝试过的:
var cust = item.Profiles.Select(c => new
{
id = c.CustId,
Name = c.Name.OrderByDescending();
}).ToList();
ViewBag.Customers = new MultiSelectList(cust, "id", "Name");
这是列表在我的视图中的显示方式:
@Html.DropDownList("id", (MultiSelectList)ViewBag.Customers, new { @class = "form-control", id = "lstCustomer" })
注意:我正在尝试按字母顺序对列表进行排序
答案 0 :(得分:0)
使用Linq:
var cust = item.Profiles.Select(c => new
{
id = c.CustId,
Name = c.Name
}).OrderByDescending(c => c.Name).ToList();
或更优雅(查询语法):
var cust = (from c in item.Profiles
orderby c.Name
select new
{
id = c.CustId,
Name = c.Name
}).ToList();
答案 1 :(得分:0)
var cust = item.Profiles.Select(c => new
{
id = c.CustId,
Name = c.Name
}).OrderByDescending(c=>c.Name).ToList();
或者
var cust = item.Profiles.OrderByDescending(a=>a.Name).Select(c => new
{
id = c.CustId,
Name = c.Name
}).ToList();
答案 2 :(得分:0)
使用查询语法执行相同的操作:
var cust = (from c in item.Profiles
orderby c.Name descending
select new
{
id = c.CustId,
Name = c.Name
}
).ToList();