我在获取地图/简化操作上遇到困难。我是C#的业余爱好者,所以部分原因可能是由于我缺乏对lambda和linq的经验,无法全面分析和理解导致错误的原因(对象创建表达式是什么)。

该代码可以完美地编译,但是在执行时会引发异常,并且我不知道哪个部分不正确。

当我禁用reduce操作时,错误仍然存​​在,因此我认为它与
地图部分。

RavenDB版本是:RavenDB-Build-573

这是例外:System.InvalidOperationException:变量
初始化选择器必须具有带有对象创建的lambda表达式
表达

我为RavenDB提供了这样的索引:

IndexCreation.CreateIndexes(typeof(FullTree).Assembly, store);


Entity对象是一个简单的类,具有一个int Id和一个int ParentId。

这是地图/缩小部分:

public class FullTreeObject
{
    public int Id { get; set; }
    public int Count { get; set; }
    public int ParentId { get; set; }
}

public class FullTree : AbstractIndexCreationTask<Entity,FullTreeObject>
   {
       public FullTree()
       {

           Map = entities => from entity in entities
                             let ids = new object[]
                               {
                                   new { entity.Id, Count = 0,
entity.ParentId },
                                   new { Id = entity.ParentId, Count
= 1, ParentId = (string)null}
                               }
                             from id in ids
                             select id;

           Reduce = results => from result in results
                               group result by result.Id into g
                               let parent = g.FirstOrDefault(x =>
x.ParentId != null)
                               select new
                               {
                                   Id = g.Key,
                                   Count = g.Sum(x => x.Count),
                                   ParentId = parent ==
(object)null ? (object)null : parent.ParentId
                               };

       }
   }


希望有人能提供一些线索。提前致谢。

-

为了响应输入,我修改了map / reduce实现。 RavenDB要求map和reduce输出具有相同的类型,但是老实说,这是不对的,因为我正在输出实体对象的值,而不是数组中获取的值。但是我无法以智识达到这些价值观。

该索引是在RavenDB Studio中创建的,但是没有返回任何结果,因此我可以假设我的实现是错误的。

public class FullTree : AbstractIndexCreationTask<Entity,FullTreeObject>
    {
        public FullTree()
        {
            Map = entities => from entity in entities
                              let ids = new object[]
                                {
                                    new { entity.Id, Count = 0, entity.ParentId },
                                    new { Id = entity.ParentId, Count = 1, ParentId = (string)null}
                                }
                              from id in ids
                              select new {
                                    entity.Id,
                                    Count = 1,
                                    entity.ParentId
                              };


            Reduce = results => from result in results
                                group result by result.Id into g
                                let parent = g.FirstOrDefault(x => x.ParentId != null)
                                select new
                                {
                                    Id = g.Key,
                                    Count = g.Sum(x => x.Count),
                                    ParentId = parent == (object)null ? (object)null : parent.ParentId
                                };
        }
    }


版本601更新:

public class FullTree : AbstractIndexCreationTask<Entity, FullTree.ReduceResult>
{

    public class ReduceResult
    {
        public int Id { get; set; }
        public int Count { get; set; }
        public int ParentId { get; set; }
    }
    public FullTree()
    {
        Map = entities => from entity in entities
                          let items = new[]
                            {
                                new { Id = (int) entity.Id, Count = (int) 0, ParentId = (int) entity.ParentId },
                                new { Id = (int) entity.ParentId, Count = (int) 1, ParentId = (int) 0}
                            }
                          from item in items
                          select new {
                                Id = item.Id,
                                Count = item.Count,
                                ParentId = item.ParentId
                          };


        Reduce = results => from result in results
                            group result by result.Id into g
                            let itemWithParent = g.FirstOrDefault(x => x.ParentId != 0)
                            select new
                            {
                                Id = g.Key,
                                Count = g.Sum(x => x.Count),
                                ParentId = (itemWithParent == null) ? (int)0 : itemWithParent.ParentId
                            };
    }
}


返回错误:
[WebException:远程服务器返回错误:(500)内部服务器错误。]
   System.Net.HttpWebRequest.GetResponse()+6111651
   Raven.Client.Connection.HttpJsonRequest.ReadStringInternal(Func`1 getResponse)在c:\ Builds \ raven \ Raven.Client.Lightweight \ Connection \ HttpJsonRequest.cs:231

[InvalidOperationException:{
  “ Url”:“ / indexes / FullTree”,
  “错误”:“ System.InvalidOperationException:无法理解查询:\ r \ n--第2行col 55:无效的NewExpression \ r \ n--第2行col 91:无法解析双精度.0.0 \ r \ n- -第2行第183行:无法解析双.0.0 \ r \ n--第2行第275行:无法解析双.0.0 \ r \ n \ r \ n在Raven.Database.Linq.QueryParsingUtils.GetVariableDeclarationForLinqMethods(字符串查询,布尔值requireSelectNewAnonymousType)在c:\ Builds \ raven-unstable \ Raven.Database \ Linq \ QueryParsingUtils.cs:第122行\ r \ n在Raven.Database.Linq.DynamicViewCompiler.TransformMapDefinitionFromLinqMethodSyntax(字符串查询,字符串和实体名称)中c:\ Builds \ raven-中的C:\ Builds \ raven不稳定\ Raven.Database \ Linq \ DynamicViewCompiler.cs:行355 \ r \ n在Raven.Database.Linq.DynamicViewCompiler.HandleMapFunction(ConstructorDeclaration ctor,String map) c:\ Builds \ raven-unstable \ Raven.Database \ Linq \ DynamicViewC中Raven.Database.Linq.DynamicViewCompiler.TransformQueryToClass()处的不稳定\ Raven.Database \ Linq \ DynamicViewCompiler.cs:行132 \ r \ n ompiler.cs:行97 \ r \ n在Raven.Database.Linq.DynamicViewCompiler.GenerateInstance()在c:\ Builds \ raven不稳定\ Raven.Database \ Linq \ DynamicViewCompiler.cs:行489 \ r \ n在Raven .Database.DocumentDatabase.PutIndex(字符串名称,IndexDefinition定义)位于Raven.Database.Server.Responders.Index.Put(c:\ Builds \ raven-unstable \ Raven.Database \ DocumentDatabase.cs:line 717 \ r \ n IHttpContext上下文,字符串索引)在c:\ Builds \ raven-unstable \ Raven.Database \ Server \ Responders \ Index.cs:line 72 \ r \ n在Raven.Database.Server.Responders.Index.Respond(IHttpContext上下文)在c:\ Builds \ raven-中的Raven.Database.Server.HttpServer.DispatchRequest(IHttpContext ctx)的c:\ Builds \ raven-unstable \ Raven.Database \ Server \ Responders \ Index.cs:line 49 \ r \ n中位于c:\ Builds \ raven-unstable \ Raven.Database \ Server \ HttpServer中Raven.Database.Server.HttpServer.HandleActualRequest(IHttpContext ctx)处的\ raven \ Raven.Database \ Server \ HttpServer.cs:行527 \ r \ n。 cs:line 303“
}]
   Raven.Client.Connection.HttpJsonRequest.ReadStringInternal(Func`1 getResponse)在c:\ Builds \ raven \ Raven.Client.Lightweight \ Connection \ HttpJsonRequest.cs:295

最佳答案

Micha,此错误现已修复。它在内部版本601中。
要查看如何实际实现所需的索引,请看一下此测试:https://github.com/ayende/ravendb/blob/master/Raven.Tests/Bugs/MapRedue/TreeWithChildrenCount.cs

10-04 21:07