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;
Problem is that I can't work out how to cast the r (with the matched relationship).
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.
There are some related SO entries about this issue, but the suggested code there either no longer works or is deprecated.
How do I retrieve the matched relationship?
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:
So this is the
ActedIn
class, with the parameterless constructor chaining a '-1' down to the base constructor.Your query then becomes:
Note, it's being cast to a
RelationshipInstance
of typeActedInPayload
notActedIn
, then, to get the data you might want:Which will give you something like:
Though obviously with different numbers on your own DB.