public static DbSelectExpression RemoveRedundantFrom(this DbSelectExpression select)
        {
            if (select.From is DbSelectExpression fromSelect)
            {
                return(DbSubqueryRemover.Remove(select, fromSelect));
            }

            return(select);
        }
Пример #2
0
        protected override Expression VisitSelect(DbSelectExpression select)
        {
            select = base.VisitSelect(select) as DbSelectExpression;

            var redundant = RedundantSubqueryGatherer.Gather(select.From);

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

            return(select);
        }
Пример #3
0
        protected override Expression VisitProjection(DbProjectionExpression proj)
        {
            proj = base.VisitProjection(proj) as DbProjectionExpression;

            if (proj.Select.From is DbSelectExpression)
            {
                var redundant = RedundantSubqueryGatherer.Gather(proj.Select);

                if (redundant != null)
                {
                    proj = DbSubqueryRemover.Remove(proj, redundant);
                }
            }

            return(proj);
        }
Пример #4
0
            protected override Expression VisitSelect(DbSelectExpression select)
            {
                var wasTopLevel = isTopLevel;

                isTopLevel = false;

                select = base.VisitSelect(select) as DbSelectExpression;

                while (CanMergeWithFrom(select, wasTopLevel))
                {
                    var fromSelect = GetLeftMostSelect(select.From);

                    select = DbSubqueryRemover.Remove(select, fromSelect);

                    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 ?? fromSelect.Skip;
                    var take    = select.Take ?? fromSelect.Take;

                    var 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 DbSelectExpression(select.Alias, select.Columns, select.From, where, orderBy, groupBy, isDistinct, skip, take, select.IsReverse);
                    }
                }

                return(select);
            }