SiberianGuy
SiberianGuy

Reputation: 25282

RavenDb Map index querying confusion

Let's assume we have the following Map index:

public class CommentsIndex : AbstractIndexCreationTask<Post>
{
   public class IndexResult
   {
      public string PostId {get;set;}
      public DateTime CommentDateTime {get;set;}
   }

   public CommentsIndex()
   {
       Map = posts => from post in posts
                      from comment in post.Comment
                      select new { PostId = post.Id, CommentDateTime = comment.DateTime }; 
   }
}

The result of this index query will be a collection of Post documents. But how can I query it by CommentDateTime? Following query will definitely not work as CommentDateTime is not part of Post document:

_documentSession.Query<Post, CommentsIndex>().Where(x => x.CommentDateTime < DateTime.UtcNow).ToList();

P. S. I know I can use live projection or calling AsProjection to shape the index query result, but I guess there should be a more natural solution for such a simple case.

Upvotes: 0

Views: 227

Answers (1)

Daniel Lang
Daniel Lang

Reputation: 6839

_documentSession.Query<CommentsIndex.IndexResult, CommentsIndex>()
    .Where(x => x.CommentDateTime < DateTime.UtcNow)
    .As<Post>()
    .ToList();

Note: there is a fundamental difference between .As<T>() and .AsProjection<T>(), as only the latter will try to get the fields and the first only casts the results.

Upvotes: 2

Related Questions