예제 #1
0
        private static ConditionalExpression BubbleUpOrs(ConditionalExpression condition)
        {
            bool modified   = false;
            bool hasChildOr = false;

            ConditionalExpression left = condition.Left as ConditionalExpression;

            if (left != null)
            {
                left        = BubbleUpOrs(left);
                modified   |= !ReferenceEquals(left, condition.Left);
                hasChildOr |= (left.Operation == ConditionalOperation.Or);
            }

            ConditionalExpression right = condition.Right as ConditionalExpression;

            if (right != null)
            {
                right       = BubbleUpOrs(right);
                modified   |= !ReferenceEquals(right, condition.Right);
                hasChildOr |= (right.Operation == ConditionalOperation.Or);
            }

            if (modified)
            {
                var newCondition = new ConditionalExpression(
                    left ?? condition.Left, condition.Operation, right ?? condition.Right);
                condition.CopyLocation(newCondition);
                condition = newCondition;
            }

            if (condition.Operation == ConditionalOperation.And && hasChildOr)
            {
                var orConditions = new List <ExpressionBase>();
                orConditions.Add(left ?? condition.Left);
                orConditions.Add(right ?? condition.Right);

                var expression = CrossMultiplyOrConditions(orConditions);
                return((ConditionalExpression)expression);
            }

            return(condition);
        }
        private static ConditionalExpression BubbleUpOrs(ConditionalExpression condition)
        {
            bool modified   = false;
            bool hasChildOr = false;

            var conditions = condition.Conditions.ToList();

            for (int i = 0; i < conditions.Count; ++i)
            {
                ConditionalExpression clause = conditions[i] as ConditionalExpression;
                if (clause != null)
                {
                    clause = BubbleUpOrs(clause);
                    if (!ReferenceEquals(clause, conditions[i]))
                    {
                        conditions[i] = clause;
                        modified      = true;
                    }

                    hasChildOr |= (clause.Operation == ConditionalOperation.Or);
                }
            }

            if (modified)
            {
                var newCondition = new ConditionalExpression(condition.Operation, conditions);
                condition.CopyLocation(newCondition);
                condition = newCondition;
            }

            if (condition.Operation == ConditionalOperation.And && hasChildOr)
            {
                var orConditions = new List <ExpressionBase>();
                orConditions.AddRange(conditions);

                var expression = CrossMultiplyOrConditions(orConditions);
                return((ConditionalExpression)expression);
            }

            return(condition);
        }