Example #1
0
        public static Expression <Func <T, bool> > BuildOperation <T>(Func <Expression, Expression, Expression> bodyFunc, string propertyName, object value)
        {
            ParameterExpression param  = Expression.Parameter(typeof(T), propertyName);
            LambdaExpression    lambda = DynamicExpression.ParseLambda(typeof(T), null, propertyName, null);

            var leftVisitor = new ReplaceExpressionVisitor(lambda.Parameters[0], param);
            var left        = leftVisitor.Visit(lambda.Body);

            ConstantExpression constant = Expression.Constant(value, lambda.Body.Type);
            var body = bodyFunc(left, constant);

            return(Expression.Lambda <Func <T, bool> >(body, param));
        }
Example #2
0
        public static Expression <Func <T, bool> > AndAlso <T>(
            this Expression <Func <T, bool> > expr1,
            Expression <Func <T, bool> > expr2)
        {
            if (expr1 == null)
            {
                return(expr2);
            }
            if (expr2 == null)
            {
                return(expr1);
            }
            var parameter = Expression.Parameter(typeof(T));

            var leftVisitor = new ReplaceExpressionVisitor(expr1.Parameters[0], parameter);
            var left        = leftVisitor.Visit(expr1.Body);

            var rightVisitor = new ReplaceExpressionVisitor(expr2.Parameters[0], parameter);
            var right        = rightVisitor.Visit(expr2.Body);

            return(Expression.Lambda <Func <T, bool> >(
                       Expression.AndAlso(left, right), parameter));
        }