所以我在这个方法中生成了一个SelectList:
public static SelectList HolidayDays()
{
SelectList retval = GenerateKeyValueList<HolidayCity>(HolidayCityHelper.GetFriendlyName, HolidayCity.NotSet);
//sort list (NY, London....then the rest in order)
return retval;
}
GenerateKeyValueList&lt;&gt;方法在这里定义:
public static SelectList GenerateKeyValueList<T>(Func<T, string> nameGetter, T itemToNoInclude) where T : struct, IComparable, IConvertible
{
List<SelectListItem> list = new List<SelectListItem>();
var enumList = Enum.GetValues(typeof(T));
foreach (var curr in enumList)
{
T t = (T)curr;
if (t.Equals(itemToNoInclude) == false)
{
list.Add(new SelectListItem() { Text = nameGetter(t), Value = Convert.ToInt32(t).ToString() });
}
}
return new SelectList(list, "Value", "Text");
}
在第一个列出的方法中,如何在注释中对列表进行排序。我希望列表将“New York”作为第一个元素,将“London”作为第二个元素,然后按字母顺序将其余元素作为元素。
答案 0 :(得分:3)
list.OrderBy(i => i.Text == "New York")
.ThenBy(i => i.Text == "London")
.ThenBy(i => i.Text);
或者你可以写:
var items = from i in list
orderby i.Text == "New York", i.Text == "London", i.Text descending
select i;