1

Im playing around with ravendb trying to create a blog.

Im using Ravens own ID meaning that a document can look like this:

BlogPosts/1
{
  "Title": "Ny post",
  "Text": "Nytt blogginlägg",
  "Kategori": "spel"
}

I fetch all my BlogPosts with this code:

var blogPosts = Session.Query<BlogPost>().ToList();

In my view, I loop through all the Blogposts like this:

 @foreach (var item in Model.BlogPosts)
                                        {
                                            <div class="media">

                                                <div class="media-body">
                                                    <h3 class="media-heading">@item.Title</h3>
                                                    <p>@item.Text</p>
                                                </div>

                                                <a class="view-info" href="#"><span>@item.Kategori</span></a>


                                            </div>
                                        }

My problem is this, everytime I create a new blogpost, the blogpost is given an ID higher than the one created before...In my view, I would like to display the blogpost with the higest number(the newest) first.

Can I use a sortOrder on this line maybe?

var blogPosts = Session.Query<BlogPost>().ToList();
Machavity
  • 30,841
  • 27
  • 92
  • 100
user2915962
  • 2,691
  • 8
  • 33
  • 60
  • Beware that `Query().ToList()` will only pull back 1024 by default. This is honouring the 'safe-by-default' principles that RavenDB is designed behind. Page the data if you need more than that. – Dominic Zukiewicz Jun 03 '14 at 12:23

1 Answers1

1

Try:

var blogPosts = Session.Query<BlogPost>().OrderByDescending(x => x.Id).ToList();

Though note that this will sort alphanumerically, so probably won't give the result you desire. See this answer from Ayende: https://stackoverflow.com/a/6149231/54222

Community
  • 1
  • 1
Simon
  • 5,373
  • 1
  • 34
  • 46