在Return中使用Sum()来计算OrderByDescending

时间:2015-09-17 16:04:58

标签: neo4j neo4jclient

我需要一些帮助,请将此查询移至Neo4jClient,我只是在与Total进行斗争。

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

到目前为止,我已经完成了这项工作,但我无法完成Total或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();

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

总计我尝试了Sum(已查看)和Sum(“已查看”),两者都以错误结束。使用OrderByDescending我无法通过尚未计算的数字(总计)来订购,所以我被困住了,请帮助。

Shaine

2 个答案:

答案 0 :(得分:2)

没有ExploreObject

MongoProperties

答案 1 :(得分:1)

以下代码会返回填充了您想要的数据的ExploreObjectReturn.As要求你设置一个Type参数(例如Return.As<int>) - 我很惊讶你一直在运行,因为它甚至没有为我编译而没有指定的类型。

记住凯斯也很重要。确保您订购的属性与查询/ db中的对象具有相同的大小写。例如,您将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();