SelectList根本没有排序

时间:2011-01-11 15:14:09

标签: c# .net sorting selectlist

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排序根本没有?它在我执行排序操作之前保持与它所处的完全相同的顺序,但它编译并运行时没有错误。

1 个答案:

答案 0 :(得分:4)

OrderByThenBy会返回一个您未设置返回值的集合;


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