使用 Return 中的 Sum() 计算 OrderByDescending

Using Sum() in Return to calculate OrderByDescending

我需要一些帮助,请将此查询移至 Neo4jClient,我只是在与总数作斗争。

MATCH (p:Product)-[viewed:VIEWED]-()
return p, count(viewed) as count, sum(viewed.total) as total
order by total desc

到目前为止,我已经做到了这一点,但我无法执行总计或 OrderByDescending。

return client.Cypher
            .Match("(product:Product)-[viewed:VIEWED]-()")
            .Return((product,viewed) => new ExploreObject
            {
                Product = product.As<Product>(),
                Count = viewed.Count(),
                Total = ???
            })
            .OrderByDescending(???)
            .Limit(18)
            .Results.ToList();

编辑:将上面的代码更改为:

return client.Cypher
            .Match("(product:Product)-[viewed:VIEWED]-()")
            .Return((product, viewed) => new
            {
                Product = product.As<Product>(),
                Count = viewed.Count(),
                Total = Return.As("sum(viewed.total)")
            })
            .Limit(18)
            .Results.ToList();

我们仍然收到错误错误:'名称 'Return' 在当前上下文中不存在'

总计我尝试了 Sum(viewed.total) 和 Sum("viewed.total") 两者都以错误结束。使用 OrderByDescending 我无法按尚未计算的数字(总计)进行排序,所以我被卡住了,请帮忙。

谢恩

没有 ExploreObject

return client.Cypher
             .Match("(product:Product)-[viewed:VIEWED]-()")
             .Return((product,viewed) => new
             {
                 Product = product.As<Product>(),
                 Count = viewed.Count(),
                 Total = Return.As("sum(viewed.total)")
             })
             .OrderByDescending(???)
             .Limit(18)
             .Results.ToList();

下面的代码将 return 您的 ExploreObject 填充您想要的数据。 Return.As 实际上要求您设置一个类型参数(例如 Return.As<int>)- 我很惊讶您竟然得到了它 运行,因为它甚至无法为我编译指定类型。

记住案例也很重要。确保您订购的属性与 query/db 中的对象具有相同的大小写。例如,您 return 将 sum(viewed.Total) 转换为一个名为 Total 的 属性,因此当您 ORDER BY 在这种情况下需要 Total不是 total。事不宜迟:

return client.Cypher
        .Match("(product:Product)-[viewed:VIEWED]-()")
        .Return((product,viewed) => new ExploreObject
        {
            Product = product.As<Product>(),
            Count = viewed.Count(),
            Total = Return.As<int>("sum(viewed.Total)") //<-- Need to define type here
        })
        .OrderByDescending("Total") //<-- Case sensitive
        .Limit(18)
        .Results.ToList();

另一条路线可能是再次使用 With

client.Cypher
    .Match("(product:Product)-[viewed:VIEWED]-()")
    .With("product, viewed, sum(viewed.Total) as summed")
    .Return((product, viewed, summed) => new ExploreObject
    {
        Product = product.As<Product>(),
        Count = viewed.Count(),
        Total = summed.As<int>()
    })
    .OrderByDescending("Total")
    .Limit(18)
    .Results.ToList();