Search code examples
c#ravendb

RavenDB index with child list


I want to create an index containing a child list from a different collection, but I'm getting "Map and Reduce functions of a index must return identical types."

This is what I got so far:

    public class PropertiesIndex : AbstractMultiMapIndexCreationTask<PropertyIndexResult>
    {
        public PropertiesIndex()
        {
            // Map for Property
            AddMap<Property>(properties => from property in properties.Where(x => x.Status < 2)
                                           select new PropertyIndexResult
                                           {
                                               Id = property.Id,
                                               Images = new List<PropertyImage>()
                                           });

            // Map for PropertyImage
            AddMap<PropertyImage>(images => from image in images
                                            select new PropertyIndexResult
                                            {
                                                Id = image.PropertyId,
                                                Images = new List<PropertyImage> { image }
                                            });
            
            // Reduce function
            Reduce = results => from result in results
                                group result by result.Id into g
                                select new PropertyIndexResult
                                {
                                    Id = g.Key,
                                    Images = g.SelectMany(x => x.Images ?? new List<PropertyImage>()).ToList()
                                };

            StoreAllFields(FieldStorage.Yes);
        }
    }

The PropertyIndexResult looks like this:

public class PropertyIndexResult
{
    public long Id { get; set; }
    public List<PropertyImage>? Images { get; set; } = [];
}

And the PropertyImage:

public class PropertyImage
{
    public long PropertyId { get; set; }
    public string? Url { get; set; }
}

I'm using ravendb 6.0.3 and .net 8.0


Solution

  • A fix to the problem is provided here https://github.com/ravendb/ravendb/pull/18880