2

Given:

var paramA = Expression.Parameter(typeof(string), "a");

and:

Expression<Func<string, bool>> expr = b => b == "Something";

Is there anyway to replace b by paramA into the expression expr?

Sebastián Guerrero
  • 1,055
  • 1
  • 19
  • 26

1 Answers1

3

You can walk the expression tree of expr, and replace all occurrences of b with paramA using the approach described in this Q&A: "Combine two lambda expressions with inner expression".

However, if you simply need a lambda expression that uses paramA as its parameter, it is easier to make a lambda that wraps expr instead:

var res = (Expression<Func<string,bool>>)Expression.Lambda(
    Expression.Invoke(expr, paramA)
,   paramA
);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523