Search code examples
c#neo4jneo4jclient

Neo4jClient return rows as arrays instead of objects?


I'm hoping this hasn't been answered before, but I searched both StackOverflow and Google a few times and couldn't get the answer to this.

Neo4jClient usually returns rows as objects of a certain type, is there any way to get each row as an array instead?

In my case I'm using a query where I'm not returning a specific object, instead I'm choosing certain attributes of nodes in relationships, e.g. (although my queries are a bit more complex and return many different attributes of many node types):

MATCH (n:User)-[:RELATIONSHIP]->(m:Message) 
RETURN n.id, n.name, m.id, m.name, m.subject

This means that I cannot simply create a C# object to encapsulate this information, since the information does not relate to a single object/node. The query might also change, and I wouldn't want to change the object definition each time.


Solution

  • You can return arrays, but obviously of one type, and you're likely going to need that type to be a string, as object won't work:

    var cypher = Client.Cypher
        .Match("(n:User)-[:RELATIONSHIP]->(m:Message) ")
        .Return<IEnumerable<string>>("[n.id,n.name,m.id,m.name,m.subject]");
    

    Your return type needs to be IEnumerable<T> where T is (most likely) a string

    var results = cypher.Results;
    foreach (var result in results)
    {
        Console.WriteLine("Result:");
        foreach (var res in result)
        {
            Console.WriteLine("\t{0}", res ?? "null");
        }
    }