我希望能够从 RavenDB 中的集合中查询前 10 个文档,该集合按计数排序,并带有子列表中的约束。这是我的实体:
public class Post
{
public string Title { get; set; }
public List<Like> Likes { get; set; }
}
public class Like
{
public DateTime Created { get; set; }
}
我尝试过以下查询:
var oneMonthAgo = DateTime.Today.AddMonths(-1);
session
.Query<Post>()
.OrderByDescending(x => x.Likes.Count(y => y.Created > oneMonthAgo))
.Take(10);
Raven 抱怨应该在索引时间而不是查询时间上完成计数。我尝试使用以下代码将计数移动到索引:
public class PostsIndex : AbstractIndexCreationTask<Post>
{
public PostsIndex()
{
var month = DateTime.Today.AddMonths(-1);
Map = posts => from doc in posts
select
new
{
doc.Title,
LikeCount = doc.Likes.Count(x => x.Created > month),
};
}
}
添加此索引时,Raven 会抛出错误 500。
该怎么办?