当使用 GridView 的内置排序与实体框架时,我可以显示外键值。例如...
<asp:boundfield HeaderText="Category" DataField="Category.Name" SortExpression="Category.Name" />
...但是当单击标题以对网格中的项目进行排序时,如何按 Category.Name 对列表列表进行排序?
我只有字符串“Category.Name”,所以我不能这样做:
.OrderBy( e => e.Category.Name )
所以我尝试了反射......
private static object GetPropertyValue( object obj, string propertyName )
{
PropertyInfo propertyInfo = obj.GetType().GetProperty( propertyName );
return propertyInfo.GetValue( obj, null );
}
// list is List<Widget>
// with a breakpoint here, ((Widget)list[i]).Companies.Name exists in all Widgets
list.OrderBy( e => GetPropertyValue( e, "Category.Name" ) )
...这是行不通的。没有抛出异常,但不按 Category.Name 排序。
有任何想法吗?