Diemauerdk
Diemauerdk

Reputation: 5928

Problems with ravendb indexes

Hi i have some trouble with ravendb indexes.

Let me explain in code what i want to do.

I have these 2 classes:

class John
{
    public string Name { get; set; }
    public IList<int> Cats { get; set; }
}

class John2
{
    public string Name { get; set; }
    public int Cat { get; set; }
}

They each represent a document type in ravendb.

I want to create a index. This index should give me as many Objects of John2 as there is entries in the Cats list of the Object of type John.

So lets say i have this:

var john = new John
{
   Name = "test",
   Cats = new List<int> { 12,13}
};

Then i want an index to do the following:

var res = john.Cats.Select(x => new John2 {Name = "john", Cat = x});

The variable res will then contain 2 entries of type John2 each with their own Cat. The number of John2 entries will then depend on the number of Cats in the Cats list on the object John.

First of all i am completely new to ravendb and indexes. So i dont even know if something like this is possible.

Does someone have suggestions on how to achieve the above functionality using raven indexes?

Upvotes: 1

Views: 128

Answers (1)

Embri
Embri

Reputation: 622

If you want a projection of your data you can use Transformers.

public class John2Transformer : AbstractTransformerCreationTask<John>
{
      public John2Transformer ()
      {
          TransformResults = johns => from john in johns
                                      from cat in john.Cats
                                      select new
                                      {
                                          Name = john.Name,
                                          Cat = cat
                                      };
      }
}

Query:

IList<John2> res = session.Query<John>()
      .TransformWith<John2Transformer, John2>()
      .ToList();

Upvotes: 2

Related Questions