Пример #1
0
        public static void ExpressionsAreEqual(this Assert assert, Expression expected, Expression actual)
        {
            var success = Lambda.ExpressionsEqual(expected, actual);

            Assert.IsTrue(success,
                          "The expressions didn't match." + NewLine + "Expected: {0}" + NewLine + "Actual: {1}",
                          expected.ToReadableString(),
                          actual.ToReadableString());
        }
Пример #2
0
        /// <summary>
        ///     Joins two binary lambda expressions using the 'And' conditional operator.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of the predicate.
        /// </typeparam>
        /// <param name="left">
        ///     The left <see cref="Expression{TDelegate}" />.
        /// </param>
        /// <param name="right">
        ///     The right <see cref="Expression{TDelegate}" />.
        /// </param>
        /// <returns>
        ///     The resulting <see cref="Expression{TDelegate}" /> object that contains the joined
        ///     <see cref="Expression{TDelegate}" /> objects.
        /// </returns>
        public static Expression <Func <T, bool> > And <T>(this Expression <Func <T, bool> > left, Expression <Func <T, bool> > right)
        {
            Expression <Func <T, bool> > all = t => true;

            if (Lambda.ExpressionsEqual(left, all))
            {
                return(right);
            }

            if (Lambda.ExpressionsEqual(right, all))
            {
                return(left);
            }

            var rightExprBody = new RebindParameterVisitor(right.Parameters[0], left.Parameters[0]).Visit(right.Body);

            return(Expression.Lambda <Func <T, bool> >(Expression.AndAlso(left.Body, rightExprBody ?? throw new InvalidOperationException()), left.Parameters));
        }