Exemple #1
0
        /// <summary>
        /// 表达式参数类型转换
        /// </summary>
        /// <typeparam name="TNew">新类型</typeparam>
        /// <param name="expression">表达式</param>
        /// <returns></returns>
        public static Expression <Func <TNew, bool> > Cast <TNew>(this LambdaExpression expression)
        {
            var candidateExpr     = Expression.Parameter(typeof(TNew), Where.ParamterName);
            var parameterReplacer = new ParameterReplacer(candidateExpr);

            var body = parameterReplacer.Replace(expression.Body);

            return(Expression.Lambda <Func <TNew, bool> >(body, candidateExpr));
        }
Exemple #2
0
        /// <summary>
        /// 或逻辑运算
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="expLeft">表达式1</param>
        /// <param name="expRight">表达式2</param>
        /// <returns></returns>
        public static Expression <Func <T, bool> > Or <T>(this Expression <Func <T, bool> > expLeft, Expression <Func <T, bool> > expRight)
        {
            var candidateExpr = Expression.Parameter(typeof(T), Where.ParamterName);
            var left          = new ParameterReplacer(candidateExpr).Replace(expLeft.Body);
            var right         = new ParameterReplacer(candidateExpr).Replace(expRight.Body);

            var body = Expression.OrElse(left, right);

            return(Expression.Lambda <Func <T, bool> >(body, candidateExpr));
        }
        public static Expression <Func <T, bool> > Or <T>(this Expression <Func <T, bool> > exp_left, Expression <Func <T, bool> > exp_right)
        {
            var candidateExpr     = Expression.Parameter(typeof(T), "candidate");
            var parameterReplacer = new ParameterReplacer(candidateExpr);

            var left  = parameterReplacer.Replace(exp_left.Body);
            var right = parameterReplacer.Replace(exp_right.Body);
            var body  = Expression.Or(left, right);

            return(Expression.Lambda <Func <T, bool> >(body, candidateExpr));
        }
        /// <summary>
        /// 将表达式参数类型转换
        /// </summary>
        /// <typeparam name="TNew">新类型</typeparam>
        /// <param name="expression">表达式</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <returns></returns>
        public static Expression <Func <TNew, bool> > Cast <TNew>(this LambdaExpression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }
            var parameter         = Expression.Parameter(typeof(TNew), Predicate.ParamterName);
            var parameterReplacer = new ParameterReplacer(parameter);

            var body = parameterReplacer.Replace(expression.Body);

            return(Expression.Lambda <Func <TNew, bool> >(body, parameter));
        }
        /// <summary>
        /// 或逻辑运算
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="expLeft">表达式1</param>
        /// <param name="expRight">表达式2</param>
        /// <returns></returns>
        public static Expression <Func <T, bool> > Or <T>(this Expression <Func <T, bool> > expLeft, Expression <Func <T, bool> > expRight)
        {
            if (expRight == null)
            {
                return(expLeft);
            }
            var parameter = Expression.Parameter(typeof(T), Predicate.ParamterName);
            var left      = new ParameterReplacer(parameter).Replace(expLeft.Body);
            var right     = new ParameterReplacer(parameter).Replace(expRight.Body);

            var body = Expression.OrElse(left, right);

            return(Expression.Lambda <Func <T, bool> >(body, parameter));
        }