Reputation: 1305
I am trying out using Neo4j in .Net with Neo4jClient. I am trying to find the best way to populate the following concrete C# classes:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
public class PersonData
{
public Person Person { get; set; }
public List<Relation> Relations { get; set; }
public List<string> Labels { get; set; }
}
public class Relation
{
public Person Relative {get; set;}
public string Relationship { get; set; }
}
I currently have the following basic graph model:
(p:Person{ id: 1, name: 'Fred', age: 42})-[r:PARENT_OF]->(c:Person{ id: 2, name: 'Sarah', age: 8})
Also with other relationship types, e.g. MARRIED_TO.
I currently have the following query, and I want to get a particular person node, and it's relations (i.e. the related person nodes and a string of what the relationship type is, which could be the relationship type or a value of the relationship), populating PersonData. I can currently easily populate Person, but I do not know how I can populate Relations.
var data = client.Cypher
.Match("(p:Person)-[r*1..1]->(per:Person)")
.Where((Person p) => p.Id == 3)
.Return((p, per) => new PersonData
{
Person = p.As<Person>()
})
.Results;
Is this population of PersonData something I will have to do outside of the query, or can it be done in the return statement?
I also have the added problem that this query is returning the node with id 3 twice, and I do not know why.
Many thanks.
Upvotes: 1
Views: 542
Reputation: 6270
This works with your classes - as long as you change from List<Person>
to IEnumerable
var query = gc.Cypher.Match("(p:Person {Id:2})")
.OptionalMatch("(p)-[r]->(p2:Person)")
.With("p, {Relationship: type(r), Relative: p2} as relations")
.Return((p, relations) => new PersonData
{
Person = p.As<Person>(),
Relations = relations.CollectAs<Relation>()
});
Upvotes: 2