关于reduce部分RavenDB索引的语法问题,平均计算

Hir*_*ist 2 reduce average ravendb ravendb-studio

我正在为求平均列的正确语法而苦苦挣扎。我所拥有的 - 来自 RavenDB Studio 编辑器:

地图:

from area in docs.Level5_AdministrativeAreas
select new 
{
     area.NAME_4,
     totalPrice = area.pricePerSquareMetre,
     areaCount = 1,
     priceAverage = 0
}
Run Code Online (Sandbox Code Playgroud)

降低:

from result in results
group result by new { result.NAME_4 } into g
select new 
{
   NAME_4 = g.Key.NAME_4,
   totalPrice = g.Sum(x => x.totalPrice),
   areaCount = g.Sum(x => x.areaCount),
   priceAverage = totalPrice / areaCount
}
Run Code Online (Sandbox Code Playgroud)

计数和总价计算正确,但我不知道如何引用totalPriceareaCount

是否需要额外的选择块?我试过“g.totalPrice”和“g.priceAverage”,但它没有被识别。

感谢您的帮助 !

Dan*_*lle 5

Reduce 部分需要是这样的:

减少

from result in results
group result by new { result.NAME_4 } into g
let theCount = g.Sum(x => x.areaCount)
let theTotal = g.Sum(x => x.totalPrice)
select new 
{
   NAME_4 = g.Key.NAME_4,
   totalPrice = theTotal,
   areaCount = theCount ,
   priceAverage = theTotal / theCount 
}
Run Code Online (Sandbox Code Playgroud)

=> 阅读MapReduce 索引的常见陷阱部分