public static Expression <Func <T, bool> > And <T>(this Expression <Func <T, bool> > exp1, Expression <Func <T, bool> > exp2) { if (exp1 == null) { return(exp2); } if (exp2 == null) { return(exp1); } // 这种拼接方式在调用的时候会报 x not found。 // 因为 表达式1 和表达式2 的 x 为不同的对象, // 而下面的拼接 使用的是表达式1的x. // return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(exp1.Body, exp2.Body), exp1.Parameters); ParameterExpression newParameter = Expression.Parameter(typeof(T), "c"); NewExpressionVisitor visitor = new NewExpressionVisitor(newParameter); var left = visitor.Replace(exp1.Body); var right = visitor.Replace(exp2.Body); var body = Expression.Add(left, right); return(Expression.Lambda <Func <T, bool> >(body, newParameter)); }
public static Expression <Func <T, bool> > Or <T>(this Expression <Func <T, bool> > expr1, Expression <Func <T, bool> > expr2) { ParameterExpression newParameter = Expression.Parameter(typeof(T), "c"); NewExpressionVisitor visitor = new NewExpressionVisitor(newParameter); var left = visitor.Replace(expr1.Body); var right = visitor.Replace(expr2.Body); var body = Expression.Or(left, right); return(Expression.Lambda <Func <T, bool> >(body, newParameter)); }
/// <summary> /// 合并表达式(Or) /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="leftExpression"></param> /// <param name="rightExpression"></param> /// <returns></returns> public static Expression <Func <TEntity, bool> > Or <TEntity>(this Expression <Func <TEntity, bool> > leftExpression, Expression <Func <TEntity, bool> > rightExpression) { ParameterExpression newParameter = Expression.Parameter(typeof(TEntity), "parameter"); NewExpressionVisitor visitor = new NewExpressionVisitor(newParameter); var left = visitor.Replace(leftExpression.Body); var right = visitor.Replace(rightExpression.Body); var body = Expression.Or(left, right); return(Expression.Lambda <Func <TEntity, bool> >(body, newParameter)); }
public static Expression <Func <T, bool> > Or <T>(this Expression <Func <T, bool> > exp1, Expression <Func <T, bool> > exp2) { if (exp1 == null) { return(exp2); } if (exp2 == null) { return(exp1); } ParameterExpression newPara = Expression.Parameter(typeof(T), "c"); NewExpressionVisitor visitor = new NewExpressionVisitor(newPara); var left = visitor.Replace(exp1.Body); var right = visitor.Replace(exp2.Body); var body = Expression.Or(left, right); return(Expression.Lambda <Func <T, bool> >(body, newPara)); }