user2509192
user2509192

Reputation: 83

How can I make an Index that simulates an inner join with RavenDB 3.5?

I've searched the internet and every potential answer to my question is either not in C# or not available with my RavenDB version.

I want to do the simpliest inner join on 2 different document types. (I know it's a document based DB and not a relational one but I'm not responsible for the current modelisation)

Let's say I have these 2 different document types:

public class FirstDocumentType
{
    public string Id { get; set; }

    public string FirstDocumentTypeProperty { get; set; }

    public string SecondDocumentTypeId { get; set; }
}

public class SecondDocumentType
{
    public string Id { get; set; }

    public string SecondDocumentProperty { get; set; }
}

I want an index that returns something like this:

public class IndexResult
{
    public string FirstDocumentTypeId { get; set; }

    public string SecondDocumentTypeId { get; set; }

    public string FirstDocumentTypeProperty { get; set; }

    public string SecondDocumentProperty { get; set; }
}

How do I do that with c#?

For RavenDB version 3.x prior to 3.5 I know it was possible to do that with a transform result within an Index constructor with something like this:

          TransformResults =
                (database, firstDocumentTypes) => from firstDocumentType in firstDocumentTypes
                    let secondDocumentType = database.Load<SecondDocumentType>(firstDocumentType.SecondDocumentTypeId)
                    select new
                    { 
                        FirstDocumentTypeId = firstDocumentType.Id,
                        SecondDocumentTypeId = secondDocumentType.Id,
                        firstDocumentType.FirstDocumentTypeProperty,
                        secondDocumentType.SecondDocumentProperty 
                    };

Now in version 3.5 the transformer needs to be in a class on it's own and I can't seem to find how to use the database to fetch the SecondDocumentType from it's id within the FirstDocumentType. The delegate function only takes 1 argument which is the document type.

EDIT: I've actually found my answer in the documentation https://ravendb.net/docs/article-page/3.5/csharp/transformers/loading-documents

I just have a hard time navigating in it...

Upvotes: 2

Views: 547

Answers (2)

user2509192
user2509192

Reputation: 83

For anyone wondering there is a function called "LoadDocument" available in AbstractTransformerCreationTask.

It can be used to simulate the INNER JOIN. Simple as that...

I've actually found my answer in the documentation https://ravendb.net/docs/article-page/3.5/csharp/transformers/loading-documents

I just have a hard time navigating in it...

Upvotes: 1

Danielle
Danielle

Reputation: 3839

I think the syntax that you are looking for can be found in this tests repository:

https://github.com/ravendb/ravendb/blob/v3.5/Raven.Tests/Indexes/TransformerParameterTest.cs

Upvotes: 2

Related Questions