0

I'm struggling with the include function in RavenDB. In my model I have a blog post that holds a list of comments. The comment class that's used in this list holds a reference to a user.

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

What I want to do is to get the blogpost and get a list of comments with the details of the user that wrote the comment to display in the UI, without querying the server for every single user (N+1).

I would be happy with some pointers on how to solve this. Thanks!

4

2 回答 2

1

I think this page would answer your question.

You can load multiple documents at once:

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);
}
于 2013-06-13T10:53:07.623 回答
1

您可以使用以下方法执行此操作:

  session.Include<BlogPost>(b=>b.Comments.Select(x=>x.UserId)).Load(1);
于 2013-06-12T08:13:19.060 回答