Is there a way to sort EntityQuery using property name strings?
Is there a way to sort EntityQuery
using property name strings?
Seriously, I have an EntityQuery
and the name of a property. I need to call OrderBy
with only the name of a property. How can I do this? Reflection gives me the following exception:
System.NotSupportedException: Method 'GetValue' on type 'System.Reflection.PropertyInfo' is not accessible. Only开发者_运维知识库 methods on primitive types, System.Math and System.Convert are supported in queries.
You have to build an expression tree like this.
Type entityType = typeof(T);
var px = Expression.Parameter(entityType,"x");
var ex = Expression.Property(px, propertyName);
var lx = Expression.Lambda<Expression<Func<T>>(ex,px);
var q //.. Entity Query
q = q.OrderBy(lx);
You should replace T with your type, this is not generics example.
精彩评论