c# - How do I return Relationship and its properties through neo4jclient with cypher -
please find below cypher query (which normal)
match (a:`entity`) - [r] - (b:`entity`) return , r , b
first question : how translate neo4jclient in c# code.
second question : relation has multiple properties. how access them using neo4jclient ?
entity has properties entityname , dataspacename relation has properties relationtype , relationfrequency.
the code have used fetching node details, below
var query1 = client .cypher .match("(a:`entity`) - [r] - (b:`entity`)") .where("a.dataspace = b.dataspace") .return((a,b) => new { fromentity = return.as<string>("a.entityname"), toentity=return.as<string>("b.entityname") } );
edit :
i had tried figure it. following worked.
var query1 = client .cypher .match("(a:`entity`) - [r] - (b:`entity`)") .where("a.dataspace = b.dataspace") .return((a,b) => new { fromentity = return.as<string>("a.entityname"), toentity=return.as<string>("b.entityname"), relation=return.as<string>("r.reltype"), } );
however, if relation wild card, namely [*] or [r *1..3], how fetch properties of relation.
relationship objects same node objects, them in same way.
how have got elements set up? 'entity', assuming have class like:
public class entity { public string dataspace { get; set; } public string entityname { get; set; } }
and relationship object:
public class relationshipobj { public string reltype { get; set; } }
you return this:
var originalquery = graphclient .cypher .match("(a:`entity`)-[r]-(b:`entity`)") .where("a.dataspace = b.dataspace") .return((a, b, r) => new { fromentity = a.as<entity>().entityname, toentity = b.as<entity>().entityname, relation = r.as<relationshipobj>().reltype, });
in situation have [r*1..3]
end more complex query you're returning enumeration instead. you're return 'relation' becomes:
relation = r.as<ienumerable<relationshipobj>>()
which can no longer pull 'reltype' property without parsing results later:
foreach (var result in results) { foreach (var relationship in result.relation) { console.writeline(relationship.reltype); } }
now, ask how cope wildcard relationship, can't properties above - don't know you're asking for. can results string
, use json.net parse right object. however, can return relationshipinstance<dictionary<string,string>>
give typekey
relationship, can use deserialize relationship object:
var query = graphclient.cypher .match("(a:`entity`)-[r*]-(b:`entity`)") .where("a.dataspace = b.dataspace") .return((a, b, r) => new { fromentity = a.as<entity>().entityname, toentity = b.as<entity>().entityname, relation = r.as<ienumerable<relationshipinstance<dictionary<string,string>>>>(), }); var results = query.results.tolist(); foreach (var result in results) { foreach (var relationship in result.relation) { if (relationship.typekey == "rel_to") { var obj = jsonconvert.deserializeobject<relationshipobj>(jsonconvert.serializeobject(relationship.data)); console.writeline(obj.reltype); } } }
we have bit of weird deserialize/serialize thing can't use relationshipobject<string>
due constraint on the generic part.
Comments
Post a Comment