Returning multi value in dynamic query using neo4j client

后端 未结 1 1935
别那么骄傲
别那么骄傲 2021-01-24 02:07

Following the question I asked: Build a dynamic query using neo4j client

I got an answer about how can I return value dynamically using string only.

When I\'m

1条回答
  •  孤独总比滥情好
    2021-01-24 02:41

    We can't get an output like in the question you asked previously - this is due to the fact that you are asking for a Node (the movie) and a Collection of strings (the collect) and they have no common properties, or even styles of property.

    Firstly, let's look at the painful way to do this:

    var q = gc.Cypher
        .Match("(movie:Movie)")
        .OptionalMatch("(movie)<-[r]-(person:Person)")
        .Return(() => Return.As("{movie:movie, roles:collect([person.name, head(split(lower(type(r)), '_')), r.roles])}"));
    
    var results = q.Results;
    

    Here we take the query items (movie, r, person) and create a type with them the {} around the results, and cast that to a string.

    This will give you a horrible string with the Node data around the movie and then a collection of the roles:

    foreach (var m in results)
    {
        //This is going to be painful to navigate/use
        dynamic d = JsonConvert.DeserializeObject(m);
        Console.WriteLine(d.movie);
        Console.WriteLine(d.roles);
    }
    

    You'd be a lot better off doing something like:

    var q = gc.Cypher
        .Match("(movie:Movie)")
        .OptionalMatch("(movie)<-[r]-(person:Person)")
        .Return(() => new
        {
            Movie = Return.As>("movie"),
            Roles = Return.As>("collect([person.name, head(split(lower(type(r)), '_')), r.roles])")
        });
    
        var res = q.Results;
    

    You could either JsonConvert.DeserializeObject() the Movie node, at your leisure, or write a strongly typed class.

    In terms of a 'dynamic' object, I don't know how you were wanting to interact with the collect part of the return statement, if this doesn't help, you might need to update the question to show a usage expectation.

    0 讨论(0)
提交回复
热议问题