Example #1
0
        protected override Expression VisitSelect(DbSelectExpression select)
        {
            select = base.VisitSelect(select) as DbSelectExpression;

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

                select = select.SetTake(skipPlusTake).SetSkip(null);
                select = select.AddRedundantSelect(this.language, new TableAlias());
                select = select.SetTake(take);
                select = DbOrderByRewriter.Rewrite(this.language, select) as DbSelectExpression;

                var inverted = select.OrderBy.Select(ob => new DbOrderExpression
                                                     (
                                                         ob.OrderType == OrderType.Ascending ? OrderType.Descending : OrderType.Ascending,
                                                         ob.Expression
                                                     ));

                select = select.SetOrderBy(inverted);
                select = select.AddRedundantSelect(this.language, new TableAlias());
                select = select.SetTake(Expression.Constant(0));
                select = DbOrderByRewriter.Rewrite(this.language, select) as DbSelectExpression;

                var reverted = select.OrderBy.Select(ob => new DbOrderExpression
                                                     (
                                                         ob.OrderType == OrderType.Ascending ? OrderType.Descending : OrderType.Ascending,
                                                         ob.Expression
                                                     ));

                select = select.SetOrderBy(reverted);
                select = select.SetTake(null);
            }

            return(select);
        }
        public static DbSelectExpression RemoveOrderExpression(this DbSelectExpression select, DbOrderExpression ordering)
        {
            if (select.OrderBy != null && select.OrderBy.Count > 0)
            {
                var orderby = new List <DbOrderExpression>(select.OrderBy);

                if (orderby != null)
                {
                    orderby.Remove(ordering);
                }

                return(select.SetOrderBy(orderby));
            }

            return(select);
        }
        public static DbSelectExpression AddOrderExpression(this DbSelectExpression select, DbOrderExpression ordering)
        {
            var orderby = new List <DbOrderExpression>();

            if (select.OrderBy != null)
            {
                orderby.AddRange(select.OrderBy);
            }

            if (orderby != null)
            {
                orderby.Add(ordering);
            }

            return(select.SetOrderBy(orderby));
        }