Reputation: 4379
I am trying to pass in a variable with a single quote to be stored in Neo4j while using Neo4jClient and I cannot seems to get the character escaping correctly.
When I use this:
var Command = new UpdateCommand()
{
Name = "3 o'clock";
};
I get an error saying there is a syntax error. When I use this:
var Command = new UpdateCommand()
{
Name = "3 o\\'clock ";
};
It is storing the variable name incorrectly with an extra escape character.
My create node is quite simple and looks like this, the newUser object has one property (Name) that is being set by the command.
_client.Cypher
.Create("(u:User {user})")
.WithParams(new { user = newUser })
.ExecuteWithoutResults();
What am I doing wrong here? What is the proper way to escape your strings with Neo4jClient?
UPDATE: My problem isn't with creating a new node as describe above but rather while doing an on create on match cypher. I was trying to simplify the question for SO and I removed the problem. Please see the below updated code and this code is throwing and error for me. It could be that I am just going about the on create / on match incorrectly as well.
_client.Cypher
.Merge("(u:User { UserId: " + userId + "})")
.OnCreate()
.Set("u = {user}")
.WithParams(new { user = newUser })
.OnMatch()
.Set(string.Format("u.Name = '{0}'",
newUser.Name))
.ExecuteWithoutResults();
Upvotes: 0
Views: 702
Reputation: 6270
You shouldn't need to do any escaping aside from standard C# (of which you don't need to for a single quote). The following code creates a new user with the name of 3 O'clock
and returns it absolutely fine.
var newUser = new User{Name = "3 O'clock"};
gc.Cypher
.Create("(u:Test {user})")
.WithParams(new {user = newUser})
.ExecuteWithoutResults();
var val = gc.Cypher
.Match("(n:Test)")
.Return(n => n.As<User>())
.Results
.First();
Console.WriteLine(val.Name);
If this code doesn't work for you - what version of Neo4jClient are you using, against what version of Neo4j, and what does this 'UpdateCommand' do?
EDIT FOR UPDATE
You should still use parameters for setting:
gc.Cypher
.Merge(string.Format("(u:User {{UserId: {0}}})", userId))
.OnCreate().Set("u = {userParam}").WithParam("userParam", newUser)
.OnMatch().Set("u.Name = {nameParam}").WithParam("nameParam", newUser.Name)
.ExecuteWithoutResults();
Let Neo4jClient deal with all the quotes / formatting of the parameters!
Upvotes: 1