Reputation: 193
I can run this query successfully in the neo4j browser and I get this result
Query: MATCH (user:User {name: 'bob'})-[:MEMBER_OF]->(group:Group)-[:INHERITS_PERMISSIONS_FROM*0..]->(parent_group:Group) WITH ( parent_group.permissions)+( group.permissions) as permissions unwind permissions as permissionslist RETURN COLLECT( DISTINCT permissionslist)
Result: ["MarketingResearch", "MarketingRead"]
How do I execute this using c#, .net core 6, Neo4jClient
If I try executing it using the Neo4jClient I get the error below:
var testquery = "(user:User {name: 'bob'})-[:MEMBER_OF]->(group:Group)-[:INHERITS_PERMISSIONS_FROM*0..]->(parent_group:Group) WITH ( parent_group.permissions)+( group.permissions) as permissions unwind permissions as permissionslist RETURN COLLECT( DISTINCT permissionslist) as userpermissions";
var resultset = graphClient.Cypher
.Match(testquery)
.Return((userpermissions) => new
{
permissions=userpermissions.As<List<string>>()
}).ResultsAsync.Result;
Exception has occurred: CLR/System.AggregateException An unhandled exception of type 'System.AggregateException' occurred in System.Private.CoreLib.dll: 'One or more errors occurred.' Inner exceptions found, see $exception in variables window for more details. Innermost exception Neo4jClient.NeoException : Neo.ClientError.Statement.SyntaxError: RETURN can only be used at the end of the query (line 1, column 223 (offset: 222)) "MATCH (user:User {name: 'bob'})-[:MEMBER_OF]->(group:Group)-[:INHERITS_PERMISSIONS_FROM*0..]->(parent_group:Group) WITH ( parent_group.permissions)+( group.permissions) as permissions unwind permissions as permissionslist RETURN COLLECT( DISTINCT permissionslist) as userpermissions"
Please advise.
Upvotes: 0
Views: 90
Reputation: 6270
The error message gives the reason, RETURN can only be used at the end of the query
, what you are doing is using two RETURN
statements, one in your testquery
and the other in your fluent query when you do .Return
.
If you want to execute a string, then I would recommend using the official driver (Neo4j.Driver) - but to use the Neo4jClient for your query - you'd need to write it like this:
var query = graphClient.Cypher
.Match("(user:User {name: 'bob'})-[:MEMBER_OF]->(group:Group)-[:INHERITS_PERMISSIONS_FROM*0..]->(parent_group:Group)")
.With("(parent_group.permissions)+( group.permissions) AS permissions")
.Unwind("permissions", "permissionslist")
.Return((permissionslist) => permissionslist.CollectAsDistinct<string>());
var results = await query.ResultsAsync;
You can see what query is actually generated by looking at the query.Query.DebugQueryText
property.
Upvotes: 0