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

            if (select.OrderBy != null)
            {
                orderBy.AddRange(select.OrderBy);
            }
            orderBy.Add(ordering);
            return(select.SetOrderBy(orderBy));
        }
        protected override Expression VisitSelect(SelectExpression select)
        {
            int num;
            int count;

            this.AddAliases(select.From);
            this.Write("SELECT ");
            if (select.IsDistinct)
            {
                this.Write("DISTINCT ");
            }
            if (select.Take != null)
            {
                this.WriteTopClause(select.Take);
            }
            this.WriteColumns(select.Columns);
            if (select.From != null)
            {
                this.WriteLine(Indentation.Same);
                this.Write("FROM ");
                this.VisitSource(select.From);
            }
            if (select.Where != null)
            {
                this.WriteLine(Indentation.Same);
                this.Write("WHERE ");
                this.VisitPredicate(select.Where);
            }
            if ((select.GroupBy != null) && (select.GroupBy.Count > 0))
            {
                this.WriteLine(Indentation.Same);
                this.Write("GROUP BY ");
                num   = 0;
                count = select.GroupBy.Count;
                while (num < count)
                {
                    if (num > 0)
                    {
                        this.Write(", ");
                    }
                    this.VisitValue(select.GroupBy[num]);
                    num++;
                }
            }
            if ((select.OrderBy != null) && (select.OrderBy.Count > 0))
            {
                this.WriteLine(Indentation.Same);
                this.Write("ORDER BY ");
                num   = 0;
                count = select.OrderBy.Count;
                while (num < count)
                {
                    OrderExpression expression = select.OrderBy[num];
                    if (num > 0)
                    {
                        this.Write(", ");
                    }
                    this.VisitValue(expression.Expression);
                    if (expression.OrderType != OrderType.Ascending)
                    {
                        this.Write(" DESC");
                    }
                    num++;
                }
            }
            return(select);
        }