Neo4jClient - 如何检查属性是否存在

时间:2015-11-12 16:47:37

标签: neo4jclient

我无法将以下Cypher查询转换为Neo4jClient语法。

MATCH n 在哪里(有(n。用户))或者n.User ="用户名" 返回

这是我目前在添加一些关系逻辑和省略HAS逻辑时所拥有的

var results = Client.Cypher
               .OptionalMatch("(result)-[connection:Connection]-(result2)")
               .Where((Result result) => result.User == username)
               .Return((result, connection, result2) => new Neo4jResultSingle()
               {
                   SearchedNode = result.As<Node<Result>>(),
                   RelationshipConnection = connection.As<RelationshipInstance<Connection>>(),
                   Relationship = connection.As<RelationshipInstance<ConnectionRelationship>>(),
                   RelationshipedNode = result2.As<Node<Result>>()
               }).Results.ToList();

2 个答案:

答案 0 :(得分:2)

查看原始查询和C#的组合可能以下内容可能会有所帮助吗?

var results = Client.Cypher
           .Match("(result)-[connection:Connection]-(result2)")
           .Where("WHERE NOT HAS(n.User) OR n.User = {username}")
           .WithParams(new { username = username })
           .Return((result, connection, result2) => new Neo4jResultSingle()
           {
               SearchedNode = result.As<Node<Result>>(),
               RelationshipConnection = connection.As<RelationshipInstance<Connection>>(),
               Relationship = connection.As<RelationshipInstance<ConnectionRelationship>>(),
               RelationshipedNode = result2.As<Node<Result>>()
           })
           .Results
           .ToList();

这会重写您的WHERE子句以包含HAS和用户名逻辑,同时对其进行参数化以缓存查询计划并防止注入。

答案 1 :(得分:1)

HAS已被EXISTS()

取代

来自最新文档:Property existence checking

MATCH (n) WHERE EXISTS(n.User) RETURN n
对于相反的匹配,

之前添加NOT
MATCH (n) WHERE NOT EXISTS(n.User) RETURN n