【问题标题】:Syntax question on reduce part of RavenDB index, average calculation关于减少 RavenDB 索引的一部分,平均计算的语法问题
【发布时间】:2020-03-01 16:21:17
【问题描述】:

我正在为平均列的正确语法而苦苦挣扎。我所拥有的——来自 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


    【解决方案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

    【讨论】:

      【解决方案2】:

      可能不太理想,但这很管用(谈不上只见树木不见森林……)

      priceAverage = g.Sum(x => x.totalPrice) / g.Sum(x => x.areaCount)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-18
        相关资源
        最近更新 更多