2

I have an entity Post which contains a child collection IList. Now I need to get 10 last comments to all posts. Here is my initial impelementation:

_documentSession.Query<Post>().SelectMany(x => x.Comments).OrderByDescending(x => x.DateTime).Take(10).ToList();

But it doesn't work as RavenDb throws exception "SelectMany is not supported".

What is the right way to impelement it using RavenDb?

SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
  • Would you mind posting your index here? I've been able to use Raven so far without using indexes, so I'd like to see how you solved this. – Bob Horn Feb 03 '12 at 14:27
  • @BobHorn, my index was pretty similar to the one provided by Thomas Freudenberg – SiberianGuy Feb 03 '12 at 16:16
  • Those links are broken. I'll do some digging... – Bob Horn Feb 03 '12 at 16:42
  • @BobHorn, here is what you are looking for: https://github.com/ayende/RaccoonBlog/blob/master/RaccoonBlog.Web/Infrastructure/Indexes/PostComments_CreationDate.cs – SiberianGuy Feb 03 '12 at 16:59
  • Thanks, Idsa. Appreciate it... – Bob Horn Feb 03 '12 at 17:10
  • @Bob you are using an index, it's just that RavenDB is creating it behind the scenes for you! See http://ayende.com/blog/4667/ravens-dynamic-queries – Matt Warren Feb 19 '12 at 15:20
  • @Matt Right, I actually do understand that Raven queries against indexes, not documents. I guess what I really meant was that I haven't had to manually create indexes myself yet. Thanks! – Bob Horn Feb 19 '12 at 15:27

1 Answers1

3

You need an index to accomplish that. The same problem was solved in RacoonBlog, see the index definition.

Thomas Freudenberg
  • 5,048
  • 1
  • 35
  • 44
  • Is an index the only way? I was able to accomplish this without an index by simply using Where(), OrderByDescending(), and Take(). As long as you're using IQueryable methods, and not IEnumerable, that should work. I worked through that issue here: http://stackoverflow.com/questions/8931289/ravendb-orderbydescending-and-take-incorrect-results. – Bob Horn Feb 02 '12 at 13:54
  • @Bob The list of comments is a property of `Post` documents, so you need an index to flatten them. – Thomas Freudenberg Feb 03 '12 at 12:17