开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜