I have an expression which was built dynamically, and I would like to apply OrderBy method but I'm struggling with this.
I have a method which is supposed to append the order clause to my pre-existing expression, but as soon as I try to build an Expression.Call it throws a runtime error that indicates that the type Queryable has not an OrderBy method.
This is my code so far:
Type type = typeof(T);
PropertyInfo property = type.GetProperty(sorts[counter].Member);
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderExpression = Expression.Lambda(propertyAccess, parameter);
expression = Expression.And(
expression,
Expression.Call(
typeof(Queryable),
"OrderBy",
new Type[] { typeof(P), property.PropertyType },
null,
Expression.Quote(orderExpression)));