Search code examples
neo4jclient

How make a Neo4jClient query with an array of params?


In original Cypher:

    MATCH (user)
    WHERE user.name IN ['Joe', 'John', 'Sara', 'Maria', 'Steve'] 
    RETURN user

something like:

    public Nodes Parse(string[] Str)
    {
        Nodes res = graphClient.Cypher.OptionalMatch("(a)-[]->(b)")    
        .Where((Vertex a) => a.Name == Parallel.ForEach<string>(Str, (s) =>  return s))   //NOT WORK! return is illegal
        .Return((a, b) => new Nodes { Source = a.As<Vertex>(), Peers = b.CollectAs<Vertex>().ToList() })
        .Results.FirstOrDefault();
        return res;
    }

And when full documentation of Neo4jClient become available ?)


Solution

  • To change your code you would do:

    public Nodes Parse(string[] str)
    {
        var query = graphClient.Cypher
            .Match("(a)-[]-?(b)")
            .Where("a.Name IN $namesParam")
            .WithParam("namesParam", str)
            .Return((a,b) => new Nodes {
                Source = a.As<Vertex>(),
                Peers = b.CollectAs<Vertex>()
            });
        var result = query.Results.FirstOrDefault();
        return result;
    }
    

    To do what your query at the top says, you would do:

    var names = new [] {"Joe", "John", "Sara", "Maria", "Steve"};
    var users = graphClient
        .Match("(user)")
        .Where("user.name IN $names")
        .WithParam("names", names)
        .Return(user => user.As<User>())
        .Results;
    

    I'm afraid there will probably never be full documentation of the client, unless other people want to contribute.