This question already has an answer here:
I want to be able to get an OrderBy
query working with a lambda expression so that I get a SQL query with the TOP(n) key word (big performance boost).
I am able to do this if I specifiy ...
PaginatedList = query.OrderBy(x => x.QuoteID).Skip(() => skipValue).Take(() => pageSize)
But because I want the orderBy field to be dynamic through a UI selection of a name I want to do something like this:
var propertyInfo = typeof(Data.Quote).GetProperty(sortName);
Expression<Func<Data.Quote, object>> orderField = x => propertyInfo.GetValue(x, null);
PaginatedList = query.OrderBy(orderField).Skip(() => skipValue).Take(() => pageSize)
This gives me the error:
"LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object)' method, and this method cannot be translated into a store expression."
I tried this that's not of type Expression<Func<T, object>>
var propertyInfo = typeof(Data.Quote).GetProperty(sortName);
Func<Data.Quote, object> orderField = x => propertyInfo.GetValue(x, null);
PaginatedList = query.OrderBy(x => orderField).Skip(() => skipValue).Take(() => pageSize)
And I get this error:
"Unable to create a constant value of type [...]. Only primitive types or enumeration types are supported in this context"
I'm sure there is a way to achieve this but at the moment not sure how.
Instead of that you need to create an expression to select that property.From this source:
Then you can order by as I show below:
Here is how to achieve what you want:
The value being copied in to
propertyInfo
is just an Object, as that is the type returned byGetProperty()
. Hover overvar
and you will confirm this.The
GetValue
method doesn't exist for an Object, so you need to cast it to the right type before making the call toGetValue
.