【发布时间】:2011-08-04 13:42:18
【问题描述】:
我有一张表,我想为每一列做排序功能。
排序有两个方向asc和desc。
1) 如何使用反射对列进行排序?
List<Person> GetSortedList(List<Person> persons, string direction, string column)
{
return persons.OrderBy(x => GetProperyByName(x, column)); //GetPropertyByName - ??
}
2) 我也想做一些我可以称之为 linq 运算符链的事情:
List<Person> GetSortedList(List<Person> persons, string direction, string column)
{
var linqChain;
if(direction=="up")
{
linqChain+=persons.OrderBy(x => GetProperyByName(x, column))
}
else
{
linqChain+=persons.OrderByDescending(x => GetProperyByName(x, column))
}
linqChain+=.Where(....);
return linqChain.Execute();
}
【问题讨论】:
-
为什么是javascript 标签?
-
web-development 也不是必需的,因为尽管您可能正在做 Web 开发,但回答问题并不需要这些知识,而且问题与它无关。跨度>
-
对不起。我只是在写js网格,忘记了这个问题只是关于排序c#列表。
-
@Henk Holterman 因为我们用来排序的 person 字段作为字符串(列)传递给排序函数。
标签: c#