Esempio n. 1
0
        protected override Expression VisitSelect(SelectExpression select)
        {
            // select * from table order by x skip s take t
            // =>
            // select * from (select top s * from (select top s + t from table order by x) order by -x) order by x

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

            if (select.Skip != null && select.Take != null && select.OrderBy.Count > 0)
            {
                Expression skip         = select.Skip;
                Expression take         = select.Take;
                Expression skipPlusTake = PartialEvaluator.Eval(Expression.Add(skip, take));

                select = select.SetTake(skipPlusTake).SetSkip(null);
                select = select.AddRedundantSelect(language, new TableAlias());
                select = select.SetTake(take);

                // propogate order-bys to new layer
                select = (SelectExpression)OrderByRewriter.Rewrite(language, select);
                IEnumerable <OrderExpression> inverted = select.OrderBy.Select(ob => new OrderExpression(ob.OrderType == OrderType.Ascending ? OrderType.Descending : OrderType.Ascending, ob.Expression));
                select = select.SetOrderBy(inverted);

                select = select.AddRedundantSelect(language, new TableAlias());
                select = select.SetTake(Expression.Constant(0)); // temporary
                select = (SelectExpression)OrderByRewriter.Rewrite(language, select);
                IEnumerable <OrderExpression> reverted = select.OrderBy.Select(ob => new OrderExpression(ob.OrderType == OrderType.Ascending ? OrderType.Descending : OrderType.Ascending, ob.Expression));
                select = select.SetOrderBy(reverted);
                select = select.SetTake(null);
            }

            return(select);
        }
Esempio n. 2
0
 public static SelectExpression RemoveOrderExpression(this SelectExpression select, OrderExpression ordering)
 {
     if (select.OrderBy != null && select.OrderBy.Count > 0)
     {
         var orderby = new List <OrderExpression>(select.OrderBy);
         orderby.Remove(ordering);
         return(select.SetOrderBy(orderby));
     }
     return(select);
 }
Esempio n. 3
0
        public static SelectExpression AddOrderExpression(this SelectExpression select, OrderExpression ordering)
        {
            var orderby = new List <OrderExpression>();

            if (select.OrderBy != null)
            {
                orderby.AddRange(select.OrderBy);
            }
            orderby.Add(ordering);
            return(select.SetOrderBy(orderby));
        }