RavenDB包括虽然集合

时间:2013-06-12 00:22:57

标签: ravendb

我正在努力使用RavenDB中的include函数。在我的模型中,我有一篇博文,其中包含一系列评论。此列表中使用的注释类包含对用户的引用。

public class BlogPost
  {
    public string Id { get; set; }
    public List<Comment> Comments { get; set; } 
    public BlogPost()
    {
        Comments = new List<Comment>();
    }
  }

public class Comment
{
    public string Id { get; set; }
    public string Text { get; set; }
    public string UserId { get; set; }
}

我想要做的是获取博客帖子并获取评论列表,其中包含编写评论的用户的详细信息,以便在UI中显示,而无需为每个用户(N + 1)查询服务器。

我会对如何解决这个问题提出一些建议。谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用以下内容执行此操作:

  session.Include<BlogPost>(b=>b.Comments.Select(x=>x.UserId)).Load(1);

答案 1 :(得分:1)

我认为this page会回答你的问题。

您可以一次加载多个文档:

var blogSpots = session.Include<BlogPost>(x => x.Comments.Select(x=>x.UserId))
    .Load("blogspot/1234", "blogspot/4321");

foreach (var blogSpot in blogSpots)
{
    foreach (var userId in blogSpot)
        // this will not require querying the server!!!
        var cust = session.Load<User>(userId);
}