Passing an expression tree as a parameter to another expression tree

I have two expression trees defined as follows:

private Expression<Func<TEntity, TPropertyResult>> PropertyAccessor { get; set; } 

and

 private Expression<Func<TPropertyResult, bool>> TestExpression { get; set; } 

I need to create a new expression tree that will lead to the equivalent:

 var expression = p => this.TestExpression(this.PropertyAccessor(p)); 

When using Expression.Invoke(this.TestExpression, this.PropertyAccessor) I get the following error

{"An expression of type 'System.Func`2 [MyEntity, System.String] cannot be used for a parameter of type' System.String '"}

TPropertyResult is a string during my test.

I tried using Expression.Call or Expression.Invoke . Bad luck. What should i use?

+6
c # lambda expression-trees
source share
1 answer

I think this does what you ask for:

 Expression<Func<TEntity, bool>> Combined { get { var entity = Expression.Parameter(typeof(TEntity)); var pa = Expression.Invoke(PropertyAccessor, entity); var te = Expression.Invoke(TestExpression, pa); return (Expression<Func<TEntity, bool>>) Expression.Lambda(te, entity); } } 

I tested this and it works as I expected.

However, after re-reading my original question (before my changes), I get the impression that you asked the wrong question and that you probably don't need expression trees. If all you need are functions, you can use them without Expression :

 private Func<TEntity, TPropertyResult> PropertyAccessor { get; set; } private Func<TPropertyResult, bool> TestExpression { get; set; } private Func<TEntity, bool> Combined { get { return entity => TestExpression(PropertyAccessor(entity)); } } 

Usage example:

 // Set up the original functions PropertyAccessor = entity => GenerateResult(entity); TestExpression = result => result.IsCool(); // This stores a reference to the combined function var fn = Combined; // This actually evaluates the function bool isCool = fn(myEntity); // Alternatively, you could evaluate the function directly, without the variable bool isCool = Combined(myEntity); 
+7
source share

All Articles