public static Expression Replace(Expression ex, IEnumerable <KeyValuePair <string, Expression> > namedParameters) { var visitor = new ReplaceVisitor(); foreach (var p in namedParameters) { visitor.NamedParams.Add(p.Key, p.Value); } return(visitor.Visit(ex)); }
public void PredicateCombineTest() { Expression <Func <RootClass, NestedClass> > lambda1 = x => x.Nested; Expression <Func <NestedClass, bool> > lambda2 = x => x.Property == 5; Expression <Func <RootClass, bool> > expected = x => x.Nested.Property == 5; var replaceVisitor = new ReplaceVisitor(lambda2, lambda1); var result = replaceVisitor.Visit(lambda2); result.ToString().Should().BeEquivalentTo(expected.ToString()); }
public void MethodCombineTest() { Expression <Func <RootClass, NestedClass> > lambda1 = x => x.Nested; Expression <Func <NestedClass, int> > lambda2 = x => Helper.GetProperty(x); Expression <Func <RootClass, int> > expected = x => Helper.GetProperty(x.Nested); var replaceVisitor = new ReplaceVisitor(lambda2, lambda1); var result = replaceVisitor.Visit(lambda2); result.ToString().Should().BeEquivalentTo(expected.ToString()); }
internal static Expression <Func <T, bool> > OrElse <T>(this Expression <Func <T, bool> > exp1, Expression <Func <T, bool> > exp2) { var parameter = Expression.Parameter(typeof(T), "t"); var leftVisitor = new ReplaceVisitor(exp1.Parameters[0], parameter); var left = leftVisitor.Visit(exp1.Body); var rightVisitor = new ReplaceVisitor(exp2.Parameters[0], parameter); var right = rightVisitor.Visit(exp2.Body); return(Expression.Lambda <Func <T, bool> >( Expression.OrElse(left, right), parameter)); }
public static Expression Replace(LambdaExpression ex, params Expression[] parameters) { var visitor = new ReplaceVisitor(); for (int i = 0; i < parameters.Length; i++) { visitor.Params.Add(ex.Parameters[i], parameters[i]); } var result = visitor.Visit(ex.Body); if (result.CanReduce) { result = result.Reduce(); } return(result); }
internal static Expression <Func <T, bool> > AndAlso <T>(this Expression <Func <T, bool> > exp1, Expression <Func <T, bool> > exp2) { if (exp1 == null) { return(exp2); } var parameter = Expression.Parameter(typeof(T)); var leftVisitor = new ReplaceVisitor(exp1.Parameters[0], parameter); var left = leftVisitor.Visit(exp1.Body); var rightVisitor = new ReplaceVisitor(exp2.Parameters[0], parameter); var right = rightVisitor.Visit(exp2.Body); return(Expression.Lambda <Func <T, bool> >( Expression.AndAlso(left, right), parameter)); }
public Expression Join(Expression beforeExpression) { if (_argumentIndex == -1) { throw new InvalidOperationException("cannot join not splitted expression"); } var arguments = new Expression[_afterExpression.Arguments.Count]; for (int i = 0; i < arguments.Length; i++) { arguments[i] = _afterExpression.Arguments[i]; } arguments[_argumentIndex] = beforeExpression; MethodCallExpression newAfterExpression = Expression.Call(_afterExpression.Object, _afterExpression.Method, arguments); var visitor = new ReplaceVisitor(_afterExpression, newAfterExpression); return(visitor.Visit(_source)); }
public static IQueryable <T> InlineMatch <T>(this IQueryable <T> e) { var visitor = new ReplaceVisitor(); return(e.Provider.CreateQuery <T>(visitor.Visit(e.Expression))); }
public static T InlineMatch <T>(this T e) where T : Expression { var visitor = new ReplaceVisitor(); return((T)visitor.Visit(e)); }
/// <summary> /// Applies an expression as argument to a lambda. /// </summary> /// <param name="lambda">A lambda expression with 1 parameter</param> /// <param name="ex">The expression to use as input for the lambda expression</param> /// <returns>The expanded body of the LambdaExpression.</returns> public static Expression ApplyTo(this LambdaExpression lambda, Expression ex) { var visitor = new ReplaceVisitor(lambda.Parameters[0], ex); return(visitor.Visit(lambda.Body)); }