RavenDB:如何修复Map Reduce代码?我在期待结果的地方获得NULL值

时间:2016-10-28 19:43:30

标签: ravendb

我有以下代码:

public class WorkOrderByUserId : AbstractMultiMapIndexCreationTask<WorkOrderByUserId.Result>
{
    public WorkOrderByUserId()
    {
        this.AddMap<WorkOrder>(items
            => from item in items
                select new Result
                {
                    OwnerId = item.OwnerId,
                    WorkOrder = new WorkOrderLookupDto
                    {
                        Id = item.Id,
                        Name = item.EventName
                    },
                    WorkOrders = null
                });

        this.AddMap<Invoice>(items
            => from item in items
                select new Result
                {
                    OwnerId = item.WorkOrder.OwnerId,
                    WorkOrder = new WorkOrderLookupDto
                    {
                        Id = item.WorkOrder.Id,
                        Name = item.WorkOrder.EventName
                    },
                    WorkOrders = null
                });

        this.Reduce = results => from result in results
            group result by result.OwnerId
            into g
            select new Result
            {
                OwnerId = g.Key,
                WorkOrders = g.Select(x => x.WorkOrder),
                WorkOrder = null
            };

        this.Indexes.Add(x => x.OwnerId, FieldIndexing.Default);
    }

    public class Result
    {
        public string OwnerId { get; set; }

        public IEnumerable<WorkOrderLookupDto> WorkOrders { get; set; }

        public WorkOrderLookupDto WorkOrder { get; set; }
    }
}

它让我非常接近我想要的地方,但是我似乎错过了一些东西,而且我失去了信息,我不知道为什么。

使用Map / Reduce Visualizer我注意到MAP正在显示结果(即填充了WorkOrders,而WorkOrder为空)

Map/Reduce Visualizer

至于这一点,我期望有一堆具有NULL WorkOrders和有效WorkOrder的项目,我希望这些项目可以减少到WorkOrders集合中。

当我查看可视化工具中的最终reduce时,我注意到我的WorkOrder为NULL并且我的WorkOrders完全丢失。

Reduce

当我查看指数的最终结果时,我看到了我正在寻找的东西,只是没有我正在寻找的实际数据。

enter image description here

我需要更改什么才能让我的最终WorkOrders不为NULL?

1 个答案:

答案 0 :(得分:0)

我能够使用以下代码获得我想要的结果:

public class WorkOrderByUserId : AbstractMultiMapIndexCreationTask<WorkOrderByUserId.Result>
{
    public WorkOrderByUserId()
    {
        this.AddMap<WorkOrder>(items
            => from item in items
                select new Result
                {
                    OwnerId = item.OwnerId,
                    WorkOrders = new[]
                    {
                        new WorkOrderLookupDto
                        {
                            Id = item.Id,
                            Name = item.EventName
                        }
                    }
                });

        this.AddMap<Invoice>(items
            => from item in items
                select new Result
                {
                    OwnerId = item.WorkOrder.OwnerId,
                    WorkOrders = new[]
                    {
                        new WorkOrderLookupDto
                        {
                            Id = item.WorkOrder.Id,
                            Name = item.WorkOrder.EventName
                        }
                    }
                });

        this.Reduce = results => from result in results
            group result by result.OwnerId
            into g
            select new Result
            {
                OwnerId = g.Key,
                WorkOrders = g.SelectMany(x => x.WorkOrders)
            };

        this.Indexes.Add(x => x.OwnerId, FieldIndexing.Default);
    }

    public class Result
    {
        public string OwnerId { get; set; }

        public IEnumerable<WorkOrderLookupDto> WorkOrders { get; set; }
    }
}