예제 #1
0
        /// <summary>
        /// Optimizes the filter contains expressions.
        /// </summary>
        /// <param name="expr">The expr.</param>
        /// <returns></returns>
        private static Expression OptimizeFilterContainsExpressions(Expression expr)
        {
            var nullEqualConditionalMatch = ExpressionUtil.MatchNullEqualConditional(expr);

            if (nullEqualConditionalMatch != null &&
                nullEqualConditionalMatch.ComparisonOperand.Type == typeof(String))
            {
                if (((MemberExpression)nullEqualConditionalMatch.ComparisonOperand).Member.Name == ((MemberExpression)((MethodCallExpression)((UnaryExpression)nullEqualConditionalMatch.IfNotNull).Operand).Object).Member.Name)
                {
                    return(ExpressionUtil.RemoveConversionsAndTypeAs(nullEqualConditionalMatch.IfNotNull));
                }
            }

            return(expr);
        }
예제 #2
0
        /// <summary>
        /// ConditionalExpression visit method
        /// </summary>
        /// <param name="c">The ConditionalExpression expression to visit</param>
        /// <returns>
        /// The visited ConditionalExpression expression
        /// </returns>
        internal override Expression VisitConditional(ConditionalExpression c)
        {
            var nullEqualConditionalMatch = ExpressionUtil.MatchNullEqualConditional(c);

            if (nullEqualConditionalMatch != null &&
                nullEqualConditionalMatch.ComparisonOperand.Type == typeof(String))
            {
                if (((MemberExpression)nullEqualConditionalMatch.ComparisonOperand).Member.Name == ((MemberExpression)((MethodCallExpression)((UnaryExpression)nullEqualConditionalMatch.IfNotNull).Operand).Object).Member.Name)
                {
                    return(ExpressionUtil.RemoveConversionsAndTypeAs(nullEqualConditionalMatch.IfNotNull));
                }
            }

            return(base.VisitConditional(c));
        }
예제 #3
0
        /// <summary>
        /// Optimizes the filter nullable boolean to condition.
        /// </summary>
        /// <param name="expr">The expr.</param>
        /// <returns></returns>
        private static Expression OptimizeFilterNullableBooleanToCondition(Expression expr)
        {
            if (expr.Type == typeof(bool))
            {
                var nullEqualConditionalMatch = ExpressionUtil.MatchNullEqualConditional(expr);
                if (nullEqualConditionalMatch != null &&
                    nullEqualConditionalMatch.ComparisonOperand.Type == typeof(bool?) &&
                    ExpressionUtil.IsFalseConstant(nullEqualConditionalMatch.IfNull) &&
                    ExpressionUtil.IsMemberAccess(nullEqualConditionalMatch.IfNotNull, "Value"))
                {
                    // (c == null) ? false : c.Value    (bool?)c
                    // =>
                    // c == (bool?)true
                    return(Expression.Equal(
                               nullEqualConditionalMatch.ComparisonOperand,
                               Expression.Constant(true, typeof(bool?))));
                }
            }

            return(expr);
        }