Search code examples
c#linqlambdaexpressionexpression-trees

How to remove Parantheses from System.Linq.Expressions.Expression?


Suppose the following class defined:

public class Person 
{
    public int Age { get; set; }
}

Now I want to build an Expression which compares the Age property to be Greather than 20, like this:

ParameterExpression param = Expression.Parameter(typeof(Person), "Person");
MemberExpression member = Expression.Property(param, "Age");
ConstantExpression constant = Expression.Constant(20);
Expression exp = Expression.GreaterThan(member, constant);

Now the exp is (Person.Age > 20)

Need it to be like Person.Age > 20

How to remove those parantheses?


Solution

  • The brackets are not really in the tree. This is just the way the built-in ToString output works. It is for debugging purposes only. The ToString output isn't even C# - it just sometimes looks like it. Try true || true to see what I mean. It is formatted as True OrElse True I believe.

    The output format can't be changed. If you require a certain output format do the formatting yourself. This can be quite a bit of work.