索引和转换器中相同 DocumentId 的多个 LoadDocument

Multiple LoadDocument for the same DocumentId in indexes and transformers

我们以RavenDb文档中的简单索引为例:

public class SampleIndex : AbstractIndexCreationTask<Invoice>
{
    public SampleIndex()
    {
        Map = invoices => from invoice in invoices
                          select new
                          {
                              CustomerId = invoice.CustomerId,
                              CustomerName = LoadDocument<Customer>(invoice.CustomerId).Name
                          };
    }
}

假设我需要在索引或转换器中拥有多个客户属性。我应该每次都调用 LoadDocument<Customer>(invoice.CustomerId).SomeProperty (可能是 RavenDb 优化它并实际加载一次文档)还是有任何特殊语法(类似于 LINQ 中的 let)?

您可以在查询中使用 .Include<Customer>(invoice => invoice.CustomerId),这样所有引用的客户都会随结果一起返回。参见 Load with Includes

LoadDocument 已缓存,因此为同一文档调用一次或多次并不重要。