/// <summary> /// 合并表达式 expr1 or expr2 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="expr1"></param> /// <param name="expr2"></param> /// <returns></returns> public static Expression <Func <T, bool> > Or <T>(this Expression <Func <T, bool> > expr1, Expression <Func <T, bool> > expr2) { if (expr1 == null) { return(expr2); } else if (expr2 == null) { return(expr1); } 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> /// 替换exp表达式的参数 /// </summary> /// <param name="exp">表达式</param> /// <param name="parameter">新参数</param> /// <returns></returns> public static Expression MergeParameter(this Expression exp, ParameterExpression parameter) { NewExpressionVisitor visitor = new NewExpressionVisitor(parameter); return(visitor.Replace(exp)); }