In Neo4j we can do: startNode(relationship), endNode(relationship)
and it will give us the start and end of the node given a relationship.
My sample query is this:
match p=(n1 {Identifier:<id>})-[:r1|r2*2]-(n2) unwind relationships(p) as rel return distinct startNode(rel) as n1, type(rel), endNode(rel) as n2
so Basically its a variable length relationships and I can get the type, as well as the start and end node of each relationship.
If I don't specify startNode or endNode I will get extra nodes that are actually not connected to each other.
This works perfectly fine in neo4j cypher but I don't know how to do startNode and endNode (Scalar functions) in c#.
Currently I have this:
var data = client.Cypher.Match("(n1)")
.Where((Node n1) => n1.Identifier == identifier)
.OptionalMatch("p=(n1)-[:r1|r1*2"]-(n2)")
.Unwind("relationships(p)", "rel")
.ReturnDistinct((n1, rel, n2) => new
{
startNode = n1.As<Node<string>>(),
endNode = n2.As<Node<string>>(),
relationship = rel.As<RelationshipInstance<object>>()
}).Results;
This is not doing the startNode and endNode functions and so I get the extra nodes-relationships that are actually not connected to each other
Any ideas on how to achieve startNode and endNode functions in c# neo4j client?
You need to use the Return.As
parts of Neo4jClient
, the below query matches your original Cypher (well, including the Optional
you use in your C#
version):
var query = client.Cypher.Match("(n1)")
.Where((Node n1) => n1.Identifier == identifier)
.OptionalMatch("p=(n1)-[:r1|r2*..2]->(n2)")
.Unwind("relationships(p)", "rel")
.ReturnDistinct((n1, rel, n2) => new
{
startNode = Return.As<Node<string>>("startNode(rel)"),
endNode = Return.As<Node<string>>("endNode(rel)"),
relationship = Return.As<string>("type(rel)")
});
The Return.As
allows you to call the functions you want to.