Let us say I have the following two expressions:
Expression<Func<T, IEnumerable<TNested>>> collectionSelector;
Expression<Func<IEnumerable<TNested>, TNested>> elementSelector;
Is there a way to "combine" these in order to form the below: (?)
Expression<Func<T, TNested>> selector;
EDIT:
Performance is very critical, so I would appreciate an optimal solution with very little overhead, if possible.
Many Thanks!
Unfortunately, I cannot access to computer ( it is bad solution in performance terms). Actually, I think that you can optimize code via call Expression.
Another way seems like that(usage auxiliary function):
Then you should create pipeline Expression via call Expression with Foo function. Like that pseudo code:
Another solution is to use
ExpressionVisitor
to replace the parameter in right expression with the whole left expression, in other words, embed the left one in the right one.Expression visitor will be quite simple, add needed data to constructor, override one method and that's all.
It can be quite easily extended to handle collections of expressions in the constructor, but I kept it short.
Now, you just need to use it on the expression bodies and construct new lambda.
I tested it on this code:
and the result is as expected:
9 something
.EDIT: If performance is your concern, why are you using expressions instead of plain
Func
s? Then you could just invoke one after another. Are the expression trees later analyzed?