public static Expression <Func <T, bool> > Or <T>(params Expression <Func <T, bool> >[] predicates)
        {
            Expression <Func <T, bool> > predicate = x => true;

            if (predicates == null || predicates.Length == 0)
            {
                return(predicate);
            }

            for (int i = 0; i < predicates.Length; i++)
            {
                if (i == 0)
                {
                    predicate = predicates[i];
                    continue;
                }

                ParameterExpression    p       = predicate.Parameters[0];
                SubstExpressionVisitor visitor = new SubstExpressionVisitor();
                visitor.subst[predicates[i].Parameters[0]] = p;
                Expression body = Expression.Or(predicate.Body, visitor.Visit(predicates[i].Body));
                predicate = Expression.Lambda <Func <T, bool> >(body, p);
            }

            return(predicate);
        }
示例#2
0
        public static Expression <Func <T, bool> > Or <T>(this Expression <Func <T, bool> > a, Expression <Func <T, bool> > b)
        {
            ParameterExpression    p       = a.Parameters[0];
            SubstExpressionVisitor visitor = new SubstExpressionVisitor {
                Subst = { [b.Parameters[0]] = p }
            };
            Expression body = Expression.OrElse(a.Body, visitor.Visit(b.Body) ?? throw new InvalidOperationException());

            return(Expression.Lambda <Func <T, bool> >(body, p));
        }
    public static Expression <Func <T, bool> > And <T>(this Expression <Func <T, bool> > a, Expression <Func <T, bool> > b)
    {
        ParameterExpression    p       = a.Parameters[0];
        SubstExpressionVisitor visitor = new SubstExpressionVisitor();

        visitor.subst[b.Parameters[0]] = p;
        Expression body = Expression.AndAlso(a.Body, visitor.Visit(b.Body));

        return(Expression.Lambda <Func <T, bool> >(body, p));
    }
示例#4
0
        public static Expression <Func <T, bool> > Or <T>(this Expression <Func <T, bool> > first, Expression <Func <T, bool> > second)
        {
            ParameterExpression p = first.Parameters[0];

            SubstExpressionVisitor visitor = new SubstExpressionVisitor();

            visitor.subst[second.Parameters[0]] = p;

            Expression body = Expression.OrElse(first.Body, visitor.Visit(second.Body));

            return(Expression.Lambda <Func <T, bool> >(body, p));
        }
示例#5
0
        public static Expression <Func <T, bool> > And <T>(this Expression <Func <T, bool> > a, Expression <Func <T, bool> > b)
        {
            var p = a.Parameters[0];

            var visitor = new SubstExpressionVisitor();

            visitor.Subst[b.Parameters[0]] = p;

            Expression body = Expression.AndAlso(a.Body, visitor.Visit(b.Body) ?? throw new InvalidOperationException());

            return(Expression.Lambda <Func <T, bool> >(body, p));
        }
        /// <summary>
        /// Or operator between expressions
        /// </summary>
        /// <param name="exprLeft"></param>
        /// <param name="exprRight"></param>
        private static Expression <Func <T, bool> > Or <T>(this Expression <Func <T, bool> > exprLeft, Expression <Func <T, bool> > exprRight)
        {
            ParameterExpression p = exprLeft.Parameters[0];

            var visitor = new SubstExpressionVisitor();

            visitor._subst[exprRight.Parameters[0]] = p;

            Expression body = Expression.OrElse(exprLeft.Body, visitor.Visit(exprRight.Body));

            return(Expression.Lambda <Func <T, bool> >(body, p));
        }