Search code examples
c#neo4jcypherneo4j-dotnet-driver

Is it possible to access Keys of a Dictionary used as a foreach loop Parameter?


I have a chyper Query which uses a Dictionary<string, double> object as a foreach loop parameter. I want to access the dictonary Keys inside the loop but this doesnt seem to work , i always get an invalid input error:

Invalid input '.' {rel.Key}

I tried the following Query:

string query = "MATCH(c: Component)  WHERE c.Name= {component}
FOREACH ( rel in {relations}| MERGE (c) -[w:WEIGHT]->(d:Component {Name={rel.Key}})
SET w.Weight={rel.Value} ))";

My Parameters are the follwing:

Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("component", component); // string
parameters.Add("relations", relations); // Dictionary<string, double>
neo4jsession.Run(query, parameters);

The only other version i can think of is using an Array of Dictionary<string, double> together with using unwind but is there any way to do it with a Dictionary and a foreach Loop?

Info: As i wrote in the Question Title i use the Neo4jDotNetDriver not the Neo4jclient


Solution

  • We'll have to adjust the syntax a bit for accessing the keys and the value for each key. The keys() function will get a list of keys for the map. When we have a single key, then we can use map[key] to access the value for that key.

    string query = "WITH {relations} as relations 
                    MATCH(c: Component)  
                    WHERE c.Name= {component}
                    FOREACH ( key in keys(relations)| 
                     MERGE (c) -[w:WEIGHT]->(d:Component {Name:key})
                     SET w.Weight = relations[key] ))";