Ejemplo n.º 1
0
 protected override Expression VisitProjection(ProjectionExpression proj)
 {
     proj = (ProjectionExpression)base.VisitProjection(proj);
     if (proj.Select.From is SelectExpression)
     {
         var redundant = RedundantSubqueryGatherer.Gather(proj.Select);
         if (redundant != null)
         {
             proj = SubqueryRemover.Remove(proj, redundant);
         }
     }
     return(proj);
 }
Ejemplo n.º 2
0
            protected override Expression VisitSelect(SelectExpression select)
            {
                var wasTopLevel = isTopLevel;

                isTopLevel = false;

                select = (SelectExpression)base.VisitSelect(select);

                // next attempt to merge subqueries that would have been removed by the above
                // logic except for the existence of a where clause
                while (CanMergeWithFrom(select, wasTopLevel))
                {
                    var fromSelect = GetLeftMostSelect(select.From);

                    // remove the redundant subquery
                    select = SubqueryRemover.Remove(select, fromSelect);

                    // merge where expressions
                    var where = select.Where;
                    if (fromSelect.Where != null)
                    {
                        if (where != null)
                        {
                            where = fromSelect.Where.And(where);
                        }
                        else
                        {
                            where = fromSelect.Where;
                        }
                    }

                    var  orderBy    = select.OrderBy != null && select.OrderBy.Count > 0 ? select.OrderBy : fromSelect.OrderBy;
                    var  groupBy    = select.GroupBy != null && select.GroupBy.Count > 0 ? select.GroupBy : fromSelect.GroupBy;
                    var  skip       = select.Skip != null ? select.Skip : fromSelect.Skip;
                    var  take       = select.Take != null ? select.Take : fromSelect.Take;
                    var  segment    = select.Segment != null ? select.Segment : fromSelect.Segment;
                    bool isDistinct = select.IsDistinct | fromSelect.IsDistinct;

                    if (where != select.Where ||
                        orderBy != select.OrderBy ||
                        groupBy != select.GroupBy ||
                        isDistinct != select.IsDistinct ||
                        skip != select.Skip ||
                        take != select.Take)
                    {
                        select = new SelectExpression(select.Alias, select.Columns, select.From, where, orderBy, groupBy, isDistinct, skip, take, segment, select.Having, select.IsReverse);
                    }
                }

                return(select);
            }
Ejemplo n.º 3
0
        protected override Expression VisitSelect(SelectExpression select)
        {
            select = (SelectExpression)base.VisitSelect(select);

            // first remove all purely redundant subqueries
            var redundant = RedundantSubqueryGatherer.Gather(select.From);

            if (redundant != null)
            {
                select = SubqueryRemover.Remove(select, redundant);
            }

            return(select);
        }