首页 > 解决方案 > 如何使用 RavenDB 3.5 创建一个模拟内部连接的索引?

问题描述

我搜索了互联网,我的问题的每个潜在答案要么不在 C# 中,要么不适用于我的 RavenDB 版本。

我想对 2 种不同的文档类型进行最简单的内部连接。(我知道它是基于文档的数据库,而不是关系数据库,但我不负责当前的建模)

假设我有这两种不同的文档类型:

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

我想要一个返回如下内容的索引:

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

    public string SecondDocumentTypeId { get; set; }

    public string FirstDocumentTypeProperty { get; set; }

    public string SecondDocumentProperty { get; set; }
}

我如何用 c# 做到这一点?

对于 3.5 之前的 RavenDB 版本 3.x,我知道可以使用 Index 构造函数中的转换结果来做到这一点,如下所示:

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

现在在 3.5 版中,转换器需要单独在一个类中,我似乎无法找到如何使用数据库从 FirstDocumentType 中的 id 获取 SecondDocumentType。委托函数只接受 1 个参数,即文档类型。

编辑:我实际上在文档https://ravendb.net/docs/article-page/3.5/csharp/transformers/loading-documents中找到了我的答案

我只是很难在其中导航...

标签: c#nosqlravendb

解决方案


我认为您正在寻找的语法可以在这个测试存储库中找到:

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


推荐阅读