Expression Tree Copy or Convert

后端 未结 3 1084
温柔的废话
温柔的废话 2020-12-08 22:55

How to convert a ExpressionTree of form

Expression> exp = p => p.Age > 50;

to

Express         


        
3条回答
  •  春和景丽
    2020-12-08 23:11

    Rough Steps:

    
     Get the expression Cast it to BinaryExpression
     Get the left operand Cast it to MemberExpression
     Get the Underlying Type that the property belong to 
     Change it to your new type if you can.
    
    

    The type you get here is a property without setter as I guessed.

    Expression> exp1 = o => this.ActualHeight>50;
    var type = ((MemberExpression)((BinaryExpression)exp1.Body).Left).Expression.Type;
    

    So you must build a new expression

    Here is the way

    manually build linq expression for x => x.Child == itemToCompare.Child

提交回复
热议问题