如何在RavenDb MapReduce中执行MaxBy
使用RavenDB教程中的如何在RavenDb MapReduce中执行MaxBy,mapreduce,ravendb,Mapreduce,Ravendb,使用RavenDB教程中的Northwind数据库,我试图按员工对订单进行分组,并为每个员工获取最新的订单 地图: 使用不存在的MaxBy进行Reduce: from result in results group result by result.Employee into grp select new { Employee = grp.Key, Count = grp.Sum(result => result.Count), MostRecent = grp.M
Northwind
数据库,我试图按员工对订单进行分组,并为每个员工获取最新的订单
地图:
使用不存在的MaxBy进行Reduce:
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
}
但是,我的reduce尝试返回0个结果
另外:RavenDB会将
Order.OrderetAt
视为适当的DateTime
值,并正确地对其进行排序吗?您需要像这样做
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,
}
1谢谢你,这很有效。2现在我看到Reduce产生了错误,但是我不理解原因。
TempMostRecent
是一个IComperable
,我认为它应该是一个系统。DateTime
,为什么它是一个IComperable
?在服务器端,日期是作为字符串保存的,也许这是相关的?
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,
}