0

我的数据模型是:

public class Category
{
    public Guid Id { get; set; }
    public List<Category> children { get; set; }
}

我想要的只是从任何级别的嵌套中获取数据。

我为此创建了一个索引:

public class CategoriesIndex : 
    AbstractIndexCreationTask<Category, CategoriesIndex.ReduceResult>
{
    public class ReduceResult
    {
        public Guid Id { get; set; }
        public Category Category { get; set; }
    }

    public CategoriesIndex()
    {
        Map = categories => 
            from category in categories
            from subcategory in category.Hierarchy(x=>x.children)
            select new {
                Id = subcategory.Id,
                Category = subcategory
            };

        Store(x => x.Id, FieldStorage.Yes);
        Store(x => x.Category, FieldStorage.Yes);
    }
}

运行该代码后,我遇到了异常

网址:“/indexes/CategoriesIndex”

System.InvalidOperationException:源代码。

怎么了?如果是我如何索引分层数据?

PS 由于某些限制,我无法更改数据模型

更新

我收到了异常的消息:

public class Index_CategoriesIndex : AbstractViewGenerator
{
public Index_CategoriesIndex()
{
    this.ViewText = @"docs.Categories.SelectMany(category => category.Hierarchy(x => x.children), (category, subcategory) => new() {
Id = subcategory.Id,
Category = subcategory
})";

    this.ForEntityNames.Add("Categories");
    this.AddMapDefinition(docs => docs.Where(__document => __document["@metadata"]["Raven-Entity-Name"] == "Categories").SelectMany((Func<dynamic, IEnumerable<dynamic>>)(category => (IEnumerable<dynamic>)(category.Hierarchy(x => x.children))), (Func<dynamic, dynamic, dynamic>)((category, subcategory) => new { Id = subcategory.Id, Category = subcategory, __document_id = category.__document_id })));
    this.AddField("Id");
    this.AddField("Category");
    this.AddField("__document_id");
}
} e:\RavenDB\Web\Tenants\RavenPortalAuth\IndexDefinitions\TemporaryIndexDefinitionsAsSource\4jy1udpm.0.cs(21,223) :
error CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
4

1 回答 1

0

您应该使用 AbstractIndexCreationTask 的 Recurse 方法。

http://ravendb.net/docs/2.0/client-api/querying/static-indexes/indexing-hierarchies

于 2013-08-12T03:55:09.947 回答