Razvan
Razvan

Reputation: 181

Load all objects of specific type from Ravendb and use include to load related documents

I am trying to load all the elements of type Cause from ravenDB and also to Include related documents contained in each specific object (in order to improve performance). For instance, I am trying to mix the following two calls together:

To load all documents:

 _session.Advanced.DocumentQuery<Cause>()
         .WhereEquals(x => x.IsDeleted, false)
         .WaitForNonStaleResultsAsOfLastWrite()
         .ToArray();

To Include related documents into the call:

_session.Include<Cause>(x => x.ValueIds)
        .Load(idCause);

Does anyone know how I could approach this problem?

Edit For anyone stumbling on this problem one possible solution is as follows:

_session.Query<Cause>()
        .Include(x => x.ValueIds)
        .Where(x => x.IsDeleted == false);

Edit 2 I encountered a different problem but it is related to the above. If I wanted to add an additional element into the Include call, does anyone know how I could achieve this? I tried the following, but I don't think it is the proper way of doing this because additional calls to the database are being made:

_session.Query<Cause>()
        .Include(x => x.ValueIds)
        .Include(x => x.GroupIds)
        .Where(x => x.IsDeleted == false);

Upvotes: 1

Views: 259

Answers (1)

Danielle
Danielle

Reputation: 3839

1) In addition to the solution you mention in 'Edit1', you can also use:

var results = session.Advanced.DocumentQuery<Cause>()
     .Include(x => x.ValueIds)
     .WhereEquals(x => x.IsDeleted, false)
     .ToList();


2) Using Multiple Includes on the same operation (as you are doing in 'Edit2') is correct.
See: https://github.com/ravendb/book/blob/v4.0/Ch02/Ch02.md#includes

Upvotes: 3

Related Questions