我有一个复杂对象的列表,并希望根据" order-by-keyword"来订购它(对象):
protected override sealed List<IndexViewModel> Order(List<IndexViewModel> listToOrder, string orderBy)
{
List<IndexViewModel> indexViewModels;
switch (orderBy.ToLower())
{
case "creator":
indexViewModels = listToOrder.OrderBy(s => s.Creator).ToList();
break;
case "startdate":
indexViewModels = listToOrder.OrderBy(s => s.StartDate).ToList();
break;
...
default:
indexViewModels = listToOrder.OrderBy(s => s.CreationDate).ToList();
break;
}
return indexViewModels;
}
有没有办法改进我的代码?可能通过将我的关键字分配给linq并让它决定如何对其进行排序?