Neo4jClient How can I return all nodes, relationships and parameters for a dynamic graph

社会主义新天地 提交于 2020-01-03 02:53:23

问题


I am building an application where node labels, parameters and relationships are going to be set by my users, so nodes, rel, and prop after a certain layer will be completely dynamic and unpredictable. My question is how do I run the following query and map the results to some sort of list that can be used to rebuild the graph visually. Using neo4jClient.

The query I use directly with the Neo4j Browser works great

Match(a:User),(b:Work)-->(n) Where a.UserId = 
'xxxx' AND  b.Name = 'CompanyA' Return b, n 

In c# I have this, but its the return part that I am stuck on.

 client.Cypher.Match("(a:User), (b:Work) --> (n)")
                .Where("(a.UserId = '" + userId + "')").AndWhere("(b.Name = name"
.Return((a,n)).Results;

Any Ideas?


回答1:


Neo4jClient can't help you directly with this, the problem (from a C# point of view) is that the client attempts to deserialize the results into concrete C# types, and you're not supplying any...

Your best bet is to do the same as in the answer to this question (use dynamic): Casting nodes of an unknown type

So, you'll end up with something like:

var query = client.Cypher
    .Match("(a:User), (b:Work) --> (n)")
    .Where("a.UserId = {userIdParam}")
    .AndWhere("b.Name = {bNameParam}")
    .WithParams( new {
        userIdParam = userId,
        bNameParam = name
    })
    .Return((a,n) => new {
        A = a.As<User>(),
        N = n.As<Node<string>>()
    });

var results = query.Results.ToList();
var nNodes = new List<dynamic>();
foreach(var result in results)
{
    nNodes.Add(JsonConvert.DeserializeObject<dynamic>(result.N.Data));
}

nNodes will then contain a collection of dynamic objects with which you can do what you want.



来源:https://stackoverflow.com/questions/31917863/neo4jclient-how-can-i-return-all-nodes-relationships-and-parameters-for-a-dynam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!