Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple example of creating and relating users together in C# with Neo4jClient.
using System;
using System.Linq;
using Neo4jClient;
using Neo4jClient.Cypher;
using Newtonsoft.Json;
public class LikesAndDislikes
#region Data objects
public enum Relationship
public abstract class Neo4jObject
protected Neo4jObject(string labels)
Labels = labels;
public string Labels { get; private set; }
public class Person : Neo4jObject
public const string TypeLabels = "Person";
public Person()
: base(TypeLabels)
public string Name { get; set; }
public class Reason
public string Value { get; set; }
#endregion Data objects
public static void Main()
var client = new GraphClient(new Uri("http://localhost.:7474/db/data"));
WhoDislikes(client, new Person {Name = "Person D"});
#region Getting
private static void WhoDislikes(IGraphClient Client, Person disliked)
var query = Client.Cypher
.Match(string.Format("(p:{0})<-[r:DISLIKES]-(other:{0})", disliked.Labels))
.Where((Person p) => p.Name == disliked.Name)
.Return((other, r) =>
Reason = r.As<Reason>().Value
var res = query.Results.ToList();
Console.WriteLine("{0} disliked by:", disliked.Name);
foreach (var result in res)
Console.WriteLine("\t{0} because {1}", result.Name, result.Reason);
#endregion Getting
#region Creation
private static void Creates(IGraphClient client)
var pA = new Person {Name = "Person A"};
var pB = new Person {Name = "Person B"};
var pC = new Person {Name = "Person C"};
var pD = new Person {Name = "Person D"};
Create(client, pA);
Create(client, pB);
Create(client, pC);
Create(client, pD);
CreateRelationship(client, pA, pB, Relationship.LIKES);
CreateRelationship(client, pB, pC, Relationship.LIKES);
CreateRelationship(client, pB, pD, Relationship.DISLIKES, new Reason {Value = "Crazy guy"});
CreateRelationship(client, pC, pD, Relationship.DISLIKES, new Reason {Value = "Don't know why..."});
CreateRelationship(client, pD, pA, Relationship.LIKES);
private static void Create(IGraphClient client, Person p)
.Create(string.Format("(p:{0} {{params}})", p.Labels))
.WithParam("params", p)
private static void CreateRelationship(IGraphClient client, Person from, Person to, Relationship relationship, Reason reason = null)
ICypherFluentQuery query = client.Cypher
.Match(string.Format("(f:{0}), (t:{1})", from.Labels, to.Labels))
.Where((Person f) => f.Name == from.Name)
.AndWhere((Person t) => t.Name == to.Name);
if (reason == null)
.CreateUnique(string.Format("(f)-[:{0}]->(t)", relationship)).ExecuteWithoutResults();
.CreateUnique(string.Format("(f)-[:{0} {{relParam}}]->(t)", relationship))
.WithParam("relParam", reason)
#endregion Creation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.