1

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)));
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Daniel
  • 2,484
  • 4
  • 27
  • 35
  • 1
    What error does it throw? What type is `P`? What is the meaning of that `And()`? Why are you passing `null` as the first parameter of the call? – svick Mar 22 '14 at 13:08
  • Yes you are right, I should not be doing this Expression.And, since I'm not adding a new condition. But how do I add .OrderBy() to an expression like (condition1 && condition2 && condition3)? @svick – Daniel Mar 25 '14 at 21:56
  • Possible duplicate of http://stackoverflow.com/questions/12219404/how-to-a-compose-a-linq-expression-to-call-orderby-on-a-set-of-entities – Frank Fajardo Jan 19 '16 at 22:35

0 Answers0