如何使用 .Net neo4j 客户端获取 属性 的 sum()?

How do get sum() of a property using the .Net neo4j client?

我使用 neo4j 2.2.1,我有这样的代码:

MATCH (n:person)-[r:BUY]->(p:product) WHERE p.name IN ['Bag','Book','Pencil'] RETURN SUM(r.total) AS st, n ORDER BY st DESC LIMIT 1

我尝试将此代码转换为 C#,

class:

public class person
{
    public string name { get; set; }
}
public class product
{
    public string name { get; set; }
}

public class buy
{
    public int total { get; set; }
}

这是我的查询

public void search1()
{
    var data1 = new[] { Bag, Book, Pencil };

    var cypher = client.Cypher
        .Match("(n:person)-[r:BUY]->(p:product)")
        .Where(" p.name IN {data1}")
        .WithParams(new { data1 })
        .Return((n, r) => new
        {
            person1 = n.As<person>(),
            buy1 = r.As<buy>(),
           })

        .OrderByDescending("sum(r.total)").Limit(1); //this is an error

    foreach (var result in cypher.Results)
    {
       result1 = result.person1.name;
       total1 =  result.buy.total; // I want to get sum(r.total) but I can't
    }

}

那么,我的查询有什么问题,我该如何解决?

我认为您只需要添加一个 WITH - 使用求和 属性 的别名 - 到您的密码语句。我有一个类似的查询,我已经这样做了并且有效。

像这样:

var cypher = client.Cypher
            .Match("(n:person)-[r:BUY]->(p:product)")
            .Where(" p.name IN {data1}")
            .WithParams(new { data1 })
            .With("a,r,sum(r.total) as sumtotal")
            .Return((a, r) => new
            {
                person1 = a.As<person>(),
                buy1 = r.As<buy>(),
               })

            .OrderByDescending("sumtotal").Limit(1);

我想你应该会发现这会如你所愿:

Cypher.Match("(n:Person)-[r:BUY]->(p:product)")
    .Where("p.name in {data1}")
    .WithParams(new {data1})
    .Return(n => new {
        Person = n.As<Person>(),
        Total = Return.As<int>("SUM(r.Total)")
    })
    .OrderByDescending("Total")
    .Limit(1)

我不知道您在原始查询中返回的 a 参数来自哪里,因为您没有在任何地方对 a 进行数学运算。