如何在RavenDb MapReduce中执行MaxBy

时间:2017-08-19 18:07:52

标签: mapreduce ravendb

使用RavenDB教程中的Northwind数据库我尝试按员工对订单进行分组,并为每位员工获取最重要的订单。

地图:

from order in docs.Orders
select new {
    Employee = order.Employee,
    Count = 1,
    MostRecent = order.OrderedAt,
    MostRecentOrderId = order.Id
}

使用不存在的MaxBy减少:

from result in results
group result by result.Employee into grp
select new {
    Employee = grp.Key,
    Count = grp.Sum(result => result.Count),
    MostRecent = grp.Max(result => result.MostRecent),
    MostRecentOrderId = grp.MaxBy(result => result.MostRecent).MostRecentOrderId,
}

减少尝试:

from result in results
group result by result.Employee into grp
let TempMostRecent = grp.Max(result => result.MostRecent)
select new {
    Employee = grp.Key,
    Count = grp.Sum(result => result.Count),
    MostRecent = TempMostRecent,
    MostRecentOrderId = grp.First(result => result.MostRecent == TempMostRecent).MostRecentOrderId
}

然而,我的减少尝试返回0结果。

另外:RavenDB会将Order.OrderetAt视为正确的DateTime值并正确排序吗?

1 个答案:

答案 0 :(得分:1)

你需要像

那样做
from order in docs.Orders
select new {
    Employee = order.Employee,
    Count = 1,
    MostRecent = order.OrderedAt,
    MostRecentOrderId = order.Id
}

from result in results
group result by result.Employee into grp
let maxOrder = grp.OrderByDescending(x=>x.MostRecent).First()
select new {
    Employee = grp.Key,
    Count = grp.Sum(result => result.Count),
    MostRecent = maxOrder.MostRecent,
    MostRecentOrderId = maxOrder.MostRecentOrderId,
}