Search code examples
c#neo4jneo4jclient

Trying to do Unwind and Create New Relationship Between Nodes Neo4J C# Client


I have a list of nodes - In the code as targetNodeList and I have a node called sourceNode(Different Types Of Nodes).

The List and the single node are already existing in the neo4j Db and I would like to make a relationship between them with additional data that is inside the targetNodeList.

TargetNodeList is a wrapper which contains the node data and the relationship data that I would like to put inside the Relationship

I havn't manage to finish the code because I don't know how to continue it but thats the sample that I tried to do :

    public void CreateRelationshipBetweenNodes(NodeType sourceNode,List<TargetNodes> targetNodeList,int solutionId)
    {
        graphClient.Cypher
            .Unwind(targetNodeList, "singleNode")
            .Match("(firstNode:FirstType)", "(secondNode:SecondType)")
            .Where(" firstNode.Id = otherNode:FirstType{Id:innerNode.Id}")
            .AndWhere(" secondNode.Id = node:SecondType {Id:singleNode.Id}")
            .WithParams(new { innerNode = sourceNode})
            .Create("(firstNode)-[msg:SENT {solution}]->(secondNode)")
            .WithParam("solution", solutionId).ExecuteWithoutResults();
    }

its not working and there is still more data that I want to add to the relationship from the singleNode for example: singleNode.Score

Would appriciate any help. Thanks a lot from advanced.


Solution

  • So I'm a little confused with the nodes you're taking in and how they relate, but hopefully the below query will get you on the right route.

    First off, match on the sourceNode then UNWIND your other nodes, ready to do the create. Once you have the two nodes MATCHed you then CREATE the relationship (PS. you may want MERGE if you don't want duplicates) - and I set an Id property on the relationship - you need to provide a property name, else it won't work!

    graphClient.Cypher
        .Match("(sourceNode:SourceNodeType {Id:sourceNode.Id})")
        .Unwind(targetNodeList, "singleNode")
        .Match("(targetNodeInDb:TargetNode {Id:targetNode.Id})")
        .Create("(sourceNode)-[:SENT {Id:{solutionIdParam}}]->(targetNode)")
        .WithParam("solutionIdParam", solutionId)
        .ExecuteWithoutResults();