public static Expression <Func <T, TResult> > ReplaceParameterByConstant <T, TResult>(Expression <Func <T, object, bool> > predicate, object constant) { var body = predicate.Body; var substitutionVisitor = new VariableSubstitutionVisitor(predicate.Parameters[1], Expression.Constant(constant, constant.GetType())); var visitedBody = substitutionVisitor.Visit(body).Reduce(); return(Expression.Lambda <Func <T, TResult> >(visitedBody, predicate.Parameters[0])); }
public static Expression <Func <T, TResult> > ReplaceParameterByConstant <T, TConstant1, TConstant2, TResult>(Expression <Func <T, TConstant1, TConstant2, bool> > predicate, TConstant1 constant1, TConstant2 constant2) { var body = predicate.Body; var substitutionVisitor = new VariableSubstitutionVisitor(predicate.Parameters[1], Expression.Constant(constant1, typeof(TConstant1))); var visitedBody = substitutionVisitor.Visit(body).Reduce(); var newLambda = Expression.Lambda <Func <T, TConstant2, TResult> >(visitedBody, predicate.Parameters[0], predicate.Parameters[2]); return(ReplaceParameterByConstant <T, TConstant2, TResult>(newLambda, constant2)); }