public static SelectList HolidayDays()
{
SelectList retval = GenerateKeyValueList<HolidayCity>(HolidayCityHelper.GetFriendlyName, HolidayCity.NotSet);
//sort
retval.OrderBy(i => i.Text == "New York")
.ThenBy(i => i.Text == "London")
.ThenBy(i => i.Text);
return retval;
}
为什么SelectList排序根本没有?它在我执行排序操作之前保持与它所处的完全相同的顺序,但它编译并运行时没有错误。
答案 0 :(得分:4)
OrderBy和ThenBy会返回一个您未设置返回值的集合;
SelectList retval = GenerateKeyValueList<HolidayCity>(HolidayCityHelper.GetFriendlyName, HolidayCity.NotSet);
return new SelectList(retval
.OrderByDescending(i => i.Text == "New York")
.ThenByDescending(i => i.Text == "London")
.ThenBy(i => i.Text).ToList(),
"Value", "Text");