protected internal virtual Expression VisitOrderBy(OrderByExpression node)
 {
     return(node.Update(
                Visit(node.Source),
                Visit(node.Clauses, VisitOrderByClause)));
 }
 protected internal virtual Expression VisitOrderBy(OrderByExpression node)
 {
     return node.Update(
         Visit(node.Source),
         Visit(node.Clauses, VisitOrderByClause));
 }
        private void TranslateOrderBy(OrderByExpression node)
        {
            Translate(node.Source);

            BsonDocument sort = new BsonDocument();
            foreach (var clause in node.Clauses)
            {
                var field = FieldExpressionFlattener.FlattenFields(clause.Expression) as IFieldExpression;
                if (field == null)
                {
                    throw new NotSupportedException("Only fields are allowed in a $sort.");
                }

                var direction = clause.Direction == SortDirection.Ascending ? 1 : -1;
                if (sort.Contains(field.FieldName))
                {
                    var message = string.Format("Redundant ordering fields are not supported: {0}.", field.FieldName);
                    throw new NotSupportedException(message);
                }

                sort.Add(field.FieldName, direction);
            }

            _stages.Add(new BsonDocument("$sort", sort));
        }
        private void VisitOrderBy(OrderByExpression node)
        {
            Visit(node.Source);

            BsonDocument sort = new BsonDocument();
            foreach (var clause in node.Clauses)
            {
                var serializationExpression = (SerializationExpression)clause.Expression;
                var info = serializationExpression.SerializationInfo;
                var name = info.ElementName;
                var direction = clause.Direction == SortDirection.Ascending ? 1 : -1;
                if (!sort.Contains(name))
                {
                    sort.Add(name, direction);
                }
                else if (sort[name] != direction)
                {
                    sort.Remove(name);
                    sort.Add(name, direction);
                }
            }

            _stages.Add(new BsonDocument("$sort", sort));
        }