RavenDb Map索引查询混乱

时间:2012-01-30 11:13:03

标签: .net ravendb

假设我们有以下Map索引:

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 }; 
   }
}

此索引查询的结果将是Post文档的集合。但是如何通过CommentDateTime查询呢?由于CommentDateTime不属于Post文档:

,因此以下查询肯定无效
_documentSession.Query<Post, CommentsIndex>().Where(x => x.CommentDateTime < DateTime.UtcNow).ToList();

P上。 S.我知道我可以使用实时投影或调用AsProjection来塑造索引查询结果,但我想对于这样一个简单的案例应该有一个更自然的解决方案。

1 个答案:

答案 0 :(得分:2)

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

注意:.As<T>().AsProjection<T>()之间存在根本区别,因为只有后者才会尝试获取字段,而第一个强制转换结果。

相关问题