使用 neo4j 客户端构建动态查询 [英] Build a dynamic query using neo4j client
问题描述
我阅读了很多关于这个主题的问题,并创建了以下几乎动态的查询:
I read many questions on this topic and created the following almost dynamic query:
var resQuery = WebApiConfig.GraphClient.Cypher
.Match("(movie:Movie {title:{title}})")
.WithParam("title", title)
.Return(() => new {
movie = Return.As<string>("movie.title")
}).Results;
不幸的是,这不是动态的,因为我在 Return 匿名类型中声明了 movie 属性.
Unfortunately this isn't dynamic since I'm declaring the movie property in the Return anonymous type.
在我发现的所有示例中,唯一的选择是将节点作为与节点属性匹配的对象返回,如:movie = Return.As
In all the examples I found the only option is to return the nodes as an object matches the node properties,
like: movie = Return.As<string>("movie.title")
我希望 Return 语句返回所有节点属性的键值对列表(它可以是任何表示形式,如 JSON 等),因为我的节点是通用的,而不是每次都来自特定的对象类型.
I want the Return statement to give me back a key-value pair list of all the node properties (it can be in any representation like JSON etc..), since my nodes are generic and not from a specific object kind every time.
这可能吗?
推荐答案
你可以这样做:
var resQuery = WebApiConfig.GraphClient.Cypher
.Match("(movie:Movie {title:{title}})")
.WithParam("title", title)
.Return(() => Return.As<Node<Dictionary<string,string>>>("movie"));
var results = resQuery.Results.Select(r => r.Data);
Console.WriteLine(results.First()["title"]);
或者,例如:
var resQuery = WebApiConfig.GraphClient.Cypher
.Match("(movie:Movie {title:{title}})")
.WithParam("title", title)
.Return(() => Return.As<Node<string>>("movie"));
var results = resQuery.Results;
List<dynamic> nodes = results.Select(r => JsonConvert.DeserializeObject<dynamic>(r.Data)).ToList();
Console.WriteLine(nodes[0].title);
这篇关于使用 neo4j 客户端构建动态查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!