I want to get a query OrderBythat works with a lambda expression to get an SQL query with the keyword TOP (n) (performance increase).
I can do this if I indicate ...
PaginatedList = query.OrderBy(x => x.QuoteID).Skip(() => skipValue).Take(() => pageSize)
But since I want the orderBy field to be dynamic by selecting a user interface, 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 an error:
"LINQ to Entities does not recognize the method 'System.Object GetValue (System.Object)', and this method cannot be translated into a storage expression."
I tried this, but not the 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:
"It is not possible to create a constant value of type [...]. Only primitive types or enumerations in this context"
, , , .