Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7071945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:44:55+00:00 2026-05-28T05:44:55+00:00

I am having difficulties getting a map / reduce operation to work. I’m a

  • 0

I am having difficulties getting a map / reduce operation to work. I’m a hobbyist with C#, so probably it is partly due to my lack of experience with lambda’s and linq to thoroughly analyse and understand the cause (what is an object create expression) leading to the error.

The code compiles perfectly, but when executed it throws an exception and I don’t understand what part is not correct.

When I disable the reduce operation the error remains, so I assume it has to do with
the Map part.

RavenDB version is: RavenDB-Build-573

This is the exception: System.InvalidOperationException: Variable
initializer select must have a lambda expression with an object create
expression

And I provide RavenDB with the index like this:

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

The Entity object is a simple class, featuring an int Id, and an int ParentId.

This is the map / reduce part:

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

       }
   }

Hope someone can provide me with some clues. Thanks in advance.

—

In reaction to the input I’ve modified the map/reduce implementation. RavenDB requires map and reduce output the same type but to be honest, this can’t be right since I’m outputing values of the entity object, and not those aquired in the array. But I cannot reach those values with intellisense.

The index is created in the RavenDB studio, but no results are returned so I can assume my implementation is faulty.

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

Update on Build 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
                            };
    }
}

Error returned:
[WebException: The remote server returned an error: (500) Internal Server Error.]
System.Net.HttpWebRequest.GetResponse() +6111651
Raven.Client.Connection.HttpJsonRequest.ReadStringInternal(Func`1 getResponse) in c:\Builds\raven\Raven.Client.Lightweight\Connection\HttpJsonRequest.cs:231

[InvalidOperationException: {
“Url”: “/indexes/FullTree”,
“Error”: “System.InvalidOperationException: Could not understand query: \r\n– line 2 col 55: invalid NewExpression\r\n– line 2 col 91: Can’t parse double .0.0\r\n– line 2 col 183: Can’t parse double .0.0\r\n– line 2 col 275: Can’t parse double .0.0\r\n\r\n at Raven.Database.Linq.QueryParsingUtils.GetVariableDeclarationForLinqMethods(String query, Boolean requiresSelectNewAnonymousType) in c:\Builds\raven-unstable\Raven.Database\Linq\QueryParsingUtils.cs:line 122\r\n at Raven.Database.Linq.DynamicViewCompiler.TransformMapDefinitionFromLinqMethodSyntax(String query, String& entityName) in c:\Builds\raven-unstable\Raven.Database\Linq\DynamicViewCompiler.cs:line 355\r\n at Raven.Database.Linq.DynamicViewCompiler.HandleMapFunction(ConstructorDeclaration ctor, String map) in c:\Builds\raven-unstable\Raven.Database\Linq\DynamicViewCompiler.cs:line 132\r\n at Raven.Database.Linq.DynamicViewCompiler.TransformQueryToClass() in c:\Builds\raven-unstable\Raven.Database\Linq\DynamicViewCompiler.cs:line 97\r\n at Raven.Database.Linq.DynamicViewCompiler.GenerateInstance() in c:\Builds\raven-unstable\Raven.Database\Linq\DynamicViewCompiler.cs:line 489\r\n at Raven.Database.DocumentDatabase.PutIndex(String name, IndexDefinition definition) in c:\Builds\raven-unstable\Raven.Database\DocumentDatabase.cs:line 717\r\n at Raven.Database.Server.Responders.Index.Put(IHttpContext context, String index) in c:\Builds\raven-unstable\Raven.Database\Server\Responders\Index.cs:line 72\r\n at Raven.Database.Server.Responders.Index.Respond(IHttpContext context) in c:\Builds\raven-unstable\Raven.Database\Server\Responders\Index.cs:line 49\r\n at Raven.Database.Server.HttpServer.DispatchRequest(IHttpContext ctx) in c:\Builds\raven-unstable\Raven.Database\Server\HttpServer.cs:line 527\r\n at Raven.Database.Server.HttpServer.HandleActualRequest(IHttpContext ctx) in c:\Builds\raven-unstable\Raven.Database\Server\HttpServer.cs:line 303”
}]
Raven.Client.Connection.HttpJsonRequest.ReadStringInternal(Func`1 getResponse) in c:\Builds\raven\Raven.Client.Lightweight\Connection\HttpJsonRequest.cs:295

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:44:56+00:00Added an answer on May 28, 2026 at 5:44 am

    Micha, the bug is now fixed. It’s within build 601.
    To see how you can actually implement an index like the want you asked for, take a look at this test: https://github.com/ayende/ravendb/blob/master/Raven.Tests/Bugs/MapRedue/TreeWithChildrenCount.cs

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having difficulties getting a span tag to work properly inside a table.
I'm having some difficulties getting the TabActivity to work. Here's the implementation of the
I'm using libxml2.2.7.3 to parse html pages and I'm having difficulties getting it work
I'm having some difficulties getting paths within SMP to work right. In the demo
I'm having difficulties getting XslCompiledTransform.Load method to take a server path. I googled around
I am having difficulties getting a subset of results From a huge set of
I'm getting acquainted with Spring OSGI and Blueprint, but am having classpath difficulties (like
I am having difficulties getting the following to export. I am running CR2008SP2 and
I've been trying to implement the jquery.safetynet plug-in but I've been having difficulties getting
I am having some difficulties getting results from a form via Perl. I believe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.