RavenDB索引的约简部分语法问题,平均计算

问题描述 投票:1回答:2

我正在为平均列使用正确的语法。我所拥有的-来自RavenDB Studio编辑器:

地图:

from area in docs.Level5_AdministrativeAreas
select new 
{
     area.NAME_4,
     totalPrice = area.pricePerSquareMetre,
     areaCount = 1,
     priceAverage = 0
}

减少:

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
}

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

是否需要额外的选择块?我尝试了“ g.totalPrice”和“ g.priceAverage”,但无法识别。

谢谢您的帮助!

average ravendb reduce ravendb-studio
2个回答
1
投票

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 
}

阅读common-pitfalls-with-mapreduce-indexes部分


0
投票

可能不是理想的,但这是可行的(谈论不为树木找森林...)

priceAverage = g.Sum(x => x.totalPrice) / g.Sum(x => x.areaCount)
© www.soinside.com 2019 - 2024. All rights reserved.