Neo4jClient-从Cypher查询中检索关系 [英] Neo4jClient - Retrieving relationship from Cypher query
问题描述
我无法从Cypher查询中检索匹配的关系。
I'm having trouble retrieving matched relationships from a Cypher query.
我有这个简单的试用代码:
I have this simple trial code:
var movie = client.Create(new Movie { Title = "The Matrix" });
client.Create(new Actor { Name = "Keanu Reeves" },
new ActedIn(movie, new ActedInPayload { Role = "Neo" }));
client.Create(new Actor { Name = "Hugo Weaving" },
new ActedIn(movie, new ActedInPayload { Role = "Agent Smith" }));
var actorsAndRoles = client
.Cypher
.Start(new { movie = movie })
.Match("actor-[r:ACTED_IN]->movie")
.Return((actor, r) => new
{
Actor = actor.As<Node<Actor>>()
// ActedIn = r.As<?????>()
})
.Results;
问题是我不知道如何转换r(具有匹配关系)。
Problem is that I can't work out how to cast the r (with the matched relationship).
尝试了各种 .As类型强制转换,但没有效果。由于我的关系类没有无参数的构造函数,因此强制转换为Relations无效。但是,Relationships基类本身没有无参数的构造函数,因此不要认为这可行。另一方面,强制转换为RelationshipReference会导致异常。完全不进行强制转换(仅返回r)会导致不支持异常。
Tried various ".As" type casts, but none work. Casting to Relationship doesn't work because my relationship class doesn't have a parameterless constructor - but then the Relationship base class doesn't have a parameterless constructor itself, so don't think that would work. Casting to RelationshipReference on the other hand causes an exception. Not casting at all (just returning r) causes a "not supported" exception.
有一些与此相关的SO条目,但是那里的建议代码不再
There are some related SO entries about this issue, but the suggested code there either no longer works or is deprecated.
如何检索匹配的关系?
推荐答案
您可以为您的关系类型创建一个无参数的构造函数,只需将其 duff数据传递给它,我知道听起来很糟糕,但是由于您实际上并不需要它,所以不会伤害了您:
You can create a parameterless constructor for your relationship type, you just pass it 'duff' data, which I know sounds bad, but as you don't really need it, it won't hurt you:
public class ActedIn : Relationship<ActedInPayload>, IRelationshipAllowingSourceNode<Actor>, IRelationshipAllowingTargetNode<Movie>
{
public ActedIn() : base(-1, null) {}
public ActedIn(NodeReference targetNode, ActedInPayload data) : base(targetNode, data) {}
public override string RelationshipTypeKey
{
get { return "ACTED_IN"; }
}
}
所以这是 ActedIn
类,无参数构造函数将 -1链接到基本构造函数。
So this is the ActedIn
class, with the parameterless constructor chaining a '-1' down to the base constructor.
您的查询将变为:
var actorsAndRoles = client
.Cypher
.Start(new {movie})
.Match("actor-[r:ACTED_IN]->movie")
.Return((actor, r) => new
{
Actor = actor.As<Node<Actor>>(),
ActedIn = r.As<RelationshipInstance<ActedInPayload>>()
})
.Results;
注意,它被强制转换为类型为<$的 RelationshipInstance
c $ c> ActedInPayload 不 ActedIn
,然后,获取您可能想要的数据:
Note, it's being cast to a RelationshipInstance
of type ActedInPayload
not ActedIn
, then, to get the data you might want:
foreach (var actorsAndRole in actorsAndRoles)
{
Console.WriteLine("{0} acted as {1} - which is in the db as {2}-[ACTED_IN]->{3}",
actorsAndRole.Actor.Data.Name,
actorsAndRole.ActedIn.Data.Role,
actorsAndRole.ActedIn.StartNodeReference.Id,
actorsAndRole.ActedIn.EndNodeReference.Id);
}
哪个会给你类似的东西:
Which will give you something like:
Keanu Reeves担任Neo-在数据库中为482- [ACTED_IN]-> 481
Keanu Reeves acted as Neo - which is in the db as 482-[ACTED_IN]->481
Hugo Weaving担任Agent Smith-在数据库中为483- [ACTED_IN]-> 481
Hugo Weaving acted as Agent Smith - which is in the db as 483-[ACTED_IN]->481
尽管在您自己的数据库中显然有不同的数字。
Though obviously with different numbers on your own DB.
这篇关于Neo4jClient-从Cypher查询中检索关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!