我使用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#,
类:
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
}
}
那么,我的查询有什么问题,我该如何解决?
答案 0 :(得分:2)
我认为您应该发现这将按您的意愿运作:
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
任何地方都没有数学。
答案 1 :(得分:1)
我认为你只需要为你的cypher语句添加一个WITH - 使用summed属性的别名。我有一个类似的查询,我已经完成了它并且它有效。
像这样:
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);