Last active
April 27, 2016 13:13
-
-
Save tekiegirl/cc4e7e2060e54788fe747ce418b6ce71 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void ClearDb(IGraphClient client) | |
{ | |
client.Cypher | |
.Match("(n)") | |
.DetachDelete("n") | |
.ExecuteWithoutResults(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void CreatePerson(IGraphClient client, params Person[] persons) | |
{ | |
client.Cypher | |
.Unwind(persons, "person") | |
.Merge("(p:Person { Id: person.Id })") | |
.OnCreate() | |
.Set("p = person") | |
.ExecuteWithoutResults(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var people = client.Cypher | |
.Match("(p:Person)") | |
.Return(p => p.As<Person>()) | |
.Results; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public int Age { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PersonData | |
{ | |
public Person Person { get; set; } | |
public IEnumerable<Relation> Relations { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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