how to allow the user to choose how to sort his list using a variable?

后端 未结 4 1166
攒了一身酷
攒了一身酷 2021-01-26 10:32

the information that the user enters can be sorted in many ways ascending and descending and i\'m trying to make the user choose how he want to see the data:

so is there

4条回答
  •  鱼传尺愫
    2021-01-26 10:55

    As I assume, el is materialized(i.e., for example, it is not just entry point to database), so you can use reflection:

    var sort = emsort.Text;
    PropertyInfo property = null;
    var sortedData = el.Select(i => new { i.ID, i.FullName })
         .OrderBy(x => 
         {
              property = property ?? x.GetType().GetProperty(sort);
              return property.GetValue(x);
         }).ToList();
    

提交回复
热议问题