简体   繁体   English

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

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

I using neo4j 2.2.1 , and I have code like this: 我使用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

and I try to convert this code to C#, 我尝试将此代码转换为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; }
}

Here is my query 这是我的查询

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
    }

}

So, what's wrong in my query, and how do I fix it? 那么,我的查询有什么问题,我该如何解决?

I think you should find this will work as you want: 我想你应该发现这将按你的意愿工作:

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)

I don't know where the a parameter you're returning in your original query is coming from, as you don't math on a anywhere. 我不知道那里的a你在原来的查询返回的参数是从哪里来的,因为你没有数学上的a任意位置。

I think you just need to add a WITH - using an alias for the summed property - to your cypher statement. 我想你只需要为你的cypher语句添加一个WITH - 使用summed属性的别名。 I've got a similar query where I've done that and it worked. 我有一个类似的查询,我已经完成了它,它工作。

Like so: 像这样:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM