简体   繁体   English

删除旧关系并在带有cypher的c#上的同一个查询中为Neo4j DB创建具有相同标签的新关系

[英]Deleting old relationships and create new ones with the same label in the same query on c# with cypher for neo4j DB

Let's suppose Location and Entity nodes. 让我们假设位置和实体节点。

I want in the same cypher query to delete all relationships (location)<-[:HAS]-() and (location)<-[:HAS]-(entity) inserts new ones. 我想在同一个密码查询中删除所有关系(位置)<-[:HAS]-()和(位置)<-[:HAS]-(实体)插入新关系。

Basically I had tried this query: 基本上我已经尝试过以下查询:

public void UpdateRelationshipOfLocation<T>(Location entityGiven, int geoNameId) where T : class, new()
        {
            var type = typeof(T).Name;

            var client = Graph.GetClient();

            client.Cypher
                .Match("location:Location")
                .Where((Location location) => location.GeoNameId == entityGiven.GeoNameId)
                .With("location")
                .Match("(location)<-[rel:HAS]-()")
                .Delete("rel")
                .With("location")
                .Match("(entity:" + type + ")")
                .Where((Admin entity) => entity.GeoNameId == geoNameId)
                .Create("(location)<-[:HAS]-(entity)")
                .ExecuteWithoutResults();
        }

Parameters passed to the query: location and geoNameId to find entity. 传递给查询的参数: locationgeoNameId以查找实体。

However, it results with no new relationships added. 但是,结果没有添加新的关系。 And no relationships deleted aswell. 并没有删除任何关系。

Any way to do it in the same query? 在同一个查询中可以做到吗?

I already got the query in Cypher can someone help me to translate do c# language? 我已经在Cypher中得到查询,有人可以帮助我翻译c#语言吗?

MATCH (l:Location {GeoNameId: 9410021})
OPTIONAL MATCH ()-[r:HAS]->(l)
DELETE r
WITH l
MATCH (a:Admin2 {GeoNameId:  2367567})
MERGE (a)-[:HAS]->(l);

You seem to be missing parentheses around your first Match call - should be something akin to this I think: 您似乎在第一个Match通话中缺少括号-应该与我的想法类似:

public void UpdateRelationshipOfLocation<T>(Location entityGiven, int geoNameId) where T : class, new()
        {
            var type = typeof(T).Name;

            var client = Graph.GetClient();

            client.Cypher
                .Match("(location:Location)") // Note parens added
                .Where((Location location) => location.GeoNameId == entityGiven.GeoNameId)
                .With("location")
                .Match("(location)<-[rel:HAS]-()")
                .Delete("rel")
                .With("location")
                .Match("(entity:" + type + ")")
                .Where((Admin entity) => entity.GeoNameId == geoNameId)
                .Create("(location)<-[:HAS]-(entity)")
                .ExecuteWithoutResults();
        }

Am assuming it's related to a similar issue you posted in this question 假设它与您在此问题中发布的类似问题有关

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

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