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> /// <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)); }
/// <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)); }