예제 #1
0
        /// <summary>
        /// 将两个Lamda表达式构建“or”的连接关系
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="exp_left">“or”的左端Lamda表达式</param>
        /// <param name="exp_right">“or”的右端Lamda表达式</param>
        /// <returns>Lamda表达式</returns>
        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);

            // 创建一个表示“OR”关系的Lamda表达式
            return(Expression.Lambda <Func <T, bool> >(body, candidateExpr));
        }
예제 #2
0
        /// <summary>
        /// 将两个Lamda表达式构建“and”的连接关系
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="exp_left">“and”的左端Lamda表达式</param>
        /// <param name="exp_right">“and”的右端Lamda表达式</param>
        /// <returns>Lamda表达式</returns>
        public static Expression <Func <T, bool> > And <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);  // 获取Lamda表达式的主体
            var right = parameterReplacer.Replace(exp_right.Body); // 获取Lamda表达式的主体
            // 创建按位AND运算的二进制Lamda表达式
            var body = Expression.And(left, right);

            // 创建一个表示“AND”关系的Lamda表达式
            return(Expression.Lambda <Func <T, bool> >(body, candidateExpr));
        }