When using GridView's built in Sorting with Entity Framework, I can display foreign key values. For example...
<asp:boundfield HeaderText="Category" DataField="Category.Name" SortExpression="Category.Name" />
...but when the header is clicked to sort the items in the grid, how can I sort my List list by Category.Name?
I only have the string "Category.Name", so I can't do this:
.OrderBy( e => e.Category.Name )
So I tried Reflection...
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" ) )
...which does not work. No exception thrown, but doesn't sort by Category.Name.
Any ideas?