public static Expression <Func <T, bool> > Or <T>( this Expression <Func <T, bool> > one, Expression <Func <T, bool> > another) { var candidateExpr = Expression.Parameter(typeof(T), "candidate"); var parameterReplacer = new ParameterReplacer(candidateExpr); var left = parameterReplacer.Replace(one.Body); var right = parameterReplacer.Replace(another.Body); var body = Expression.Or(left, right); return(Expression.Lambda <Func <T, bool> >(body, candidateExpr)); }
public static Expression <Func <T, bool> > And <T>(this Expression <Func <T, bool> > one, Expression <Func <T, bool> > another) { // 首先定义好一个ParameterExpression var candidateExpr = Expression.Parameter(typeof(T), "candidate"); var parameterReplacer = new ParameterReplacer(candidateExpr); // 将表达式树的参数统一替换成我们定义好的candidateExpr var left = parameterReplacer.Replace(one.Body); var right = parameterReplacer.Replace(another.Body); var body = Expression.And(left, right); return(Expression.Lambda <Func <T, bool> >(body, candidateExpr)); }
public FilterBuilder <TEntity> Or(Expression <Func <TEntity, bool> > expr) { if (_result == null) { _result = expr; return(this); } var candidateExpr = Expression.Parameter(typeof(TEntity), "t"); var parameterReplacer = new ParameterReplacer(candidateExpr); var left = parameterReplacer.Replace(_result.Body); var right = parameterReplacer.Replace(expr.Body); var body = Expression.Or(left, right); _result = Expression.Lambda <Func <TEntity, bool> >(body, candidateExpr); return(this); }
public FilterBuilder <TEntity> And(Expression <Func <TEntity, bool> > expr) { if (_result == null) { _result = expr; return(this); } // 首先定义好一个ParameterExpression var candidateExpr = Expression.Parameter(typeof(TEntity), "t"); var parameterReplacer = new ParameterReplacer(candidateExpr); // 将表达式树的参数统一替换成我们定义好的candidateExpr var left = parameterReplacer.Replace(_result.Body); var right = parameterReplacer.Replace(expr.Body); var body = Expression.And(left, right); _result = Expression.Lambda <Func <TEntity, bool> >(body, candidateExpr); return(this); }