0

I am trying to write a program that generates a list of Days of the Week that are all linked together. in the form (Monday)-> (Tuesday)-> (Wednesday)... Although I am able to do this by writing the Cypher queries directly through the web interface, I cannot seem to do this programmatically in C# using GraphDatabase.Driver. The query to create Day 1 seems to work, but for some reason my second query is not working. Here is the code

string[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

using (var driver = GraphDatabase.Driver("bolt://localhost:7687", AuthTokens.Basic(<my_username>, <my_password>)))
        using (var session = driver.Session())
        {

            session.Run("CREATE (d1: Day {day: {Day1}}) ", new Dictionary<string, object> { { "Day1", days[0] }});

            for (int i = 1; i < days.Length-2; i++)
            {
                session.Run("" +
                    "Match (d :Day {day:'{day1} '}) " +
                    "WITH d " +
                    " CREATE (d)-[:before] -> (d2 :Day {day:'{day2}'}) ", new Dictionary<string, object> { { "day1", days[i-1] }, { "day2", days[i] } });
            }
        }
Gabor Szarnyas
  • 4,410
  • 3
  • 18
  • 42
Justin Yapp
  • 89
  • 1
  • 6

1 Answers1

0

You should not add apostrophes around parameters. In your first CREATE query, this rule is kept, but in the second query, '{day1} ' and '{day2}' should not be surrounded by apostrophes.

You can also create the whole linked list in a single Cypher command:

WITH ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] as days
UNWIND days AS day
CREATE (:Day {day: day})
WITH DISTINCT days
UNWIND range(0, length(days) - 2) AS i
MATCH (d1:Day {day: days[i]}), (d2:Day {day: days[i+1]})
CREATE (d1)-[:before]->(d2)

This first creates the nodes for the days (7 in total), then iterates through a set of indices to create the relationships (6).

Gabor Szarnyas
  • 4,410
  • 3
  • 18
  • 42