[英]Neo4j Cypher to C# how to create different relationships with other nodes based on the value of a parameter
I am trying to create something like this: 我正在尝试创建这样的东西:
Match (rt:Root{Name:root})
Match (o:Organization{orgName:"Org1"})
create (u:User{FirstName:"User1", UserRole:"is_member"})
CASE
When u.UserRole="is_member" Then Create (u) -[:"is_member"] -> (o)
ELSE u.UserRole="is_guest" Then Create (u) -[:"is_guest"] -> (o)
ELSE u.UserRole="is_admin" Then Create (u) -[:"is_admin"] -> (rt)
So depending on UserRole, different relationships will be created between the user and other nodes. 因此,根据UserRole,将在用户和其他节点之间创建不同的关系。 For some reason, this is not working in Neo4j and also what would be the equivalent in C#
由于某些原因,这在Neo4j中不起作用,并且在C#中是等效的
Thank you! 谢谢!
You have several syntax errors. 您有几个语法错误。
The following query should work (note: I opted to use MERGE
instead of CREATE
to avoid creating duplicates in case you repeat the same query): 以下查询应该可以工作(注意:我选择使用
MERGE
而不是CREATE
以避免在重复相同查询的情况下创建重复项):
MATCH (rt:Root{Name:root}), (o:Organization{orgName:"Org1"})
MERGE (u:User{FirstName:"User1", UserRole:"is_member"})
WITH rt, o, u,
CASE u.UserRole
WHEN "is_member" THEN {m: [1]}
WHEN "is_guest" THEN {g: [1]}
WHEN "is_admin" THEN {a: [1]}
END AS todo
FOREACH(x IN todo.m | MERGE (u) -[:is_member]-> (o) )
FOREACH(x IN todo.g | MERGE (u) -[:is_guest]-> (o) )
FOREACH(x IN todo.a | MERGE (u) -[:is_admin]-> (rt) )
A CASE expression can only generate variables -- it cannot do operations like CREATE
or MERGE
. CASE表达式只能生成变量-它不能执行
CREATE
或MERGE
类的操作。 Although the above query also contains a CASE
clause, it just generates a todo
variable whose value is either an object or NULL
(if none of the WHEN
tests are satisfied). 尽管上面的查询还包含一个
CASE
子句,但它只是生成一个todo
变量,其值可以是一个对象或NULL
(如果不满足WHEN
测试)。
Unlike a CASE
expression, a FOREACH clause can perform CREATE
or MERGE
operations. 与
CASE
表达式不同, FOREACH子句可以执行CREATE
或MERGE
操作。 So, the above query has 3 of them -- but at most one of them will actually perform a MERGE
, since a FOREACH(x IN todo.foo | ...)
clause does nothing unless todo.foo
exists and is a non-empty list. 因此,上面的查询有3个-但其中最多一个实际上会执行
MERGE
,因为FOREACH(x IN todo.foo | ...)
子句不执行任何操作,除非todo.foo
存在且为非-空列表。 (We don't need to use the x
variables, so they are ignored.) (我们不需要使用
x
变量,因此将忽略它们。)
Also: The APOC procedure apoc.do.case was recently added, and theoretically could be used instead of the above CASE
and FOREACH
approach. 另外:最近添加了APOC过程apoc.do.case ,理论上可以代替上面的
CASE
和FOREACH
方法使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.