2

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.

Ianvdl
  • 57
  • 8

1 Answers1

3

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");
    }
}
Charlotte Skardon
  • 6,220
  • 2
  • 31
  • 42
  • This seems to be working fine, thank you. Oddly enough, it seems my original idea would have worked, but I didn't have the `[]` inside the `Return()` method. Could you explain why those are needed? I didn't need them when I tested the query on the Neo4j web interface. – Ianvdl Jun 05 '14 at 17:35
  • The web interface is capable of showing the data, and formats it to look like an array, C# needs the code to be defined as an actual array to get a result. – Charlotte Skardon Jun 05 '14 at 18:42