简体   繁体   English

如何使用Neo4jClient获取关系的startNodeId和endNodeId?

[英]How get relationship's startNodeId and endNodeId using Neo4jClient?

var query = client.Cypher
             .Match("(n1)-[r]-[n2]")
             .Where("n1.NodeId={startNodeId} and n2.NodeId={endNodeId}")
             .WithParam("startNodeId",startNodeId)
             .withParam("endNodeId",endNodeId)
             .return((r,n1,n2)=>new {Edge=r.As<Node<string>>(),Type=r.Type()});

By this way,I can only get the relationship's Label and Properties .But I also want to get the relationship's startNodeId and endNodeId. 通过这种方式,我只能获取关系的LabelProperties 。但是我也想获取关系的startNodeId和endNodeId。 By the way,I can get the relationship's startNodeId and endNodeId by using REST API. 顺便说一句,我可以使用REST API获取关系的startNodeId和endNodeId。 How can anyone help me? 谁能帮助我?

Z.Tom 汤姆

If you know the classes which you want to cast to you can use: 如果您知道要转换为的类,则可以使用:

var results= client.Cypher
             .Match("(n1)-[r]-[n2]")
             .Where("n1.NodeId={startNodeId} and n2.NodeId={endNodeId}")
             .WithParam("startNodeId",startNodeId)
             .withParam("endNodeId",endNodeId)
.Return((n1, r1 n2) => new {Edge=r.As<Node<string>>(),Type=r.Type()}, Class1= n1.As<Class1>(), Class2= n2.As<Class2>()})
.Results;

Then you can iterate through the results. 然后,您可以遍历结果。 If only one result is expected its first item in the 'results' array 如果仅预期一个结果,则它是“结果”数组中的第一项

It depends on what you're meaning by startNodeId - if you mean the Neo4j Ids, then you'll be wanting to use the Relationship<T> class: 这取决于startNodeId含义-如果要表示Neo4j Ids,则需要使用Relationship<T>类:

client.Cypher
    .Match("(n1)-[r]-(n2)")
    //Your where stuff here.
    .Return(r => r.As<RelationshipInstance<string>());

The T should be a type you've put on the relationship, but if you haven't got that, just use object , you will need to use the Relationship<T> type, you can't use the non-generic version. T应该是您放置在关系上的一种类型,但是如果您还没有做到这一点,只需使用object需要使用Relationship<T>类型,就不能使用非通用版本。

If the startNodeId is a property on a type, then you either do r => r.As<YourType>() or r => r.As<Relationship<YourType>>() . 如果startNodeId是类型的属性,则可以执行r => r.As<YourType>()r => r.As<Relationship<YourType>>()

As a side note, you can use Neo4jClient s ability to parse parameters and save some code (also make it more compile safe) with respect to your Where clauses. 附带说明一下,您可以使用Neo4jClient的功能来解析参数并保存一些与Where子句相关的代码(还可以使其安全编译)。

I would write your code as: 我会将您的代码写为:

var query = client.Cypher
    .Match("(n1)-[r]-[n2]")
    .Where((NodeType n1) => n1.NodeId == startNodeId)
    .AndWhere((NodeType n2) => n2.NodeId == endNodeId)
    .Return(r => new { Edge = r.As<RelationshipInstance<object>>(), Type = r.Type()});

which auto parses the parameters. 自动解析参数。

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

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