Skip to content

Instantly share code, notes, and snippets.

@tekiegirl
Last active Apr 27, 2016
Embed
What would you like to do?
private void ClearDb(IGraphClient client)
{
client.Cypher
.Match("(n)")
.DetachDelete("n")
.ExecuteWithoutResults();
}
private void CreatePerson(IGraphClient client, params Person[] persons)
{
client.Cypher
.Unwind(persons, "person")
.Merge("(p:Person { Id: person.Id })")
.OnCreate()
.Set("p = person")
.ExecuteWithoutResults();
}
private void CreateUniqueRelationship(int person1Id, int person2Id, string relType, bool twoWay, GraphClient client)
{
var query = client.Cypher
.Match("(p1:Person)", "(p2:Person)")
.Where((Person p1) => p1.Id == person1Id)
.AndWhere((Person p2) => p2.Id == person2Id)
.CreateUnique("(p1)-[:" + relType + "]->(p2)");
if(twoWay)
query = query.CreateUnique("(p1)<-[:" + relType + "]-(p2)");
query.ExecuteWithoutResults();
}
var query = client.Cypher
.Match("(p:Person {Id:{id}})")
.WithParams(new { 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>()
});
var result = query.Results;
var people = client.Cypher
.Match("(p:Person)")
.Return(p => p.As<Person>())
.Results;
var client = new GraphClient(new Uri("http://localhost:7474/db/data"), "neo4j", "myPa55w0rd");
client.Connect();
// clear database
ClearDb(client);
var father = new Person { Id = 1, Name = "Paul", Age = 45};
var mother = new Person { Id = 2, Name = "Julia", Age = 43 };
var son = new Person { Id = 3, Name = "Hugo", Age = 5 };
var daughter = new Person { Id = 4, Name = "Mary", Age = 7 };
CreatePerson(client, father, mother, son, daughter);
CreateUniqueRelationship(father.Id, mother.Id, "MARRIED_TO", true, client);
CreateUniqueRelationship(father.Id, son.Id, "PARENT_OF", false, client);
CreateUniqueRelationship(father.Id, daughter.Id, "PARENT_OF", false, client);
CreateUniqueRelationship(mother.Id, son.Id, "PARENT_OF", false, client);
CreateUniqueRelationship(mother.Id, daughter.Id, "PARENT_OF", false, client);
CreateUniqueRelationship(son.Id, father.Id, "CHILD_OF", false, client);
CreateUniqueRelationship(son.Id, mother.Id, "CHILD_OF", false, client);
CreateUniqueRelationship(daughter.Id, father.Id, "CHILD_OF", false, client);
CreateUniqueRelationship(daughter.Id, mother.Id, "CHILD_OF", false, client);
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 IEnumerable<Relation> Relations { get; set; }
}
public class Relation
{
public Person Relative {get; set;}
public string Relationship { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment