简体   繁体   English

如何在C#中使用Neo4jClient在两个节点之间添加关系

[英]How to Add a relationship between two nodes using Neo4jClient with C#

public void AddRelationshipTest()
    {
        var conn = Neo4JConn.Connect();
        Team obj = new Team();
        int id1 = 1, id2 = 2;
        NodeReference<Team> sTm = (NodeReference<Team>)ds.GetNodeReference<Team>(id1, conn, obj);
        NodeReference<Team> dTm = (NodeReference<Team>)ds.GetNodeReference<Team>(id2, conn, obj);

        ReportsToData data = new ReportsToData();
        data.SinceYear = 2008;
        reportsTo relation = new reportsTo(dTm, data);
        conn.CreateRelationship(sTm, relation);



    }

where Get NodeRefernce is a method which returns reference of the node and that is working fine. 其中,Get NodeRefernce是一种返回节点引用的方法,并且运行良好。 Team is the class having data members as of the nodes When i am using CreateRelationship it creates a relationship My main concern is that it creates relationship even if there is a relationship already between source and target node. 团队是具有节点数据成员的类。当我使用CreateRelationship时,它会创建关系。我主要关心的是,即使源节点和目标节点之间已经存在关系,它也会创建关系。 I want to create a unique relationship in between two nodes just like 我想在两个节点之间创建唯一关系,就像

graphClient.Cypher
.Match("(user1:User)", "(user2:User)")
.Where((User user1) => user1.Id == 123)
.AndWhere((User user2) => user2.Id == 456)
.CreateUnique("user1-[:FRIENDS_WITH]->user2")
.ExecuteWithoutResults();

but here FRIENDS_WITH (any relationship) I have to hard code it so i don't want to use it. 但是在这里FRIENDS_WITH(任何关系)我必须对其进行硬编码,所以我不想使用它。

How to do it with Neo4jClient? 如何使用Neo4jClient?

I'm not really sure what the problem is, with the .CreateUnique why not just use string.Format or string interpolation to not hard code the relationship? 我不确定是什么问题,为什么使用.CreateUnique而不只是使用string.Format或字符串插值来不对关系进行硬编码?

public void CreateRelationship(IGraphClient graphClient, string relationshipName)
{
    graphClient.Cypher
        .Match("(user1:User)", "(user2:User)")
        .Where((User user1) => user1.Id == 123)
        .AndWhere((User user2) => user2.Id == 456)
        .CreateUnique($"(user1)-[:{relationshipName}]->(user2)") //<-- here
        .ExecuteWithoutResults();
}

obviously that could be: 显然这可能是:

.CreateUnique(string.Format("(user1)-[:{0}]->(user2)", relationshipName))

or even just use + if you really want: 或者,如果您确实想要,甚至可以使用+

.CreateUnique("(user1)-[:" + relationshipName + "]->(user2)")

You should definitely be using the Cypher technique though, and not using NodeReference<T> 你绝对应该使用Cypher支架技术虽然和使用NodeReference<T>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM