Search code examples
c#neo4jneo4jclient

Convert cypher query to c#


I have two tables:

 User
   Name
   Surname

 Phone
   number
   type_of_number



var myList = ((IRawGraphClient) client).ExecuteGetCypherResults<**i_need_class_to_save_it**>(
                    new CypherQuery("match (a:User)-[r]->(b:Phone) return a,collect(b)", null, CypherResultMode.Set))
                .Select(un => un.Data);

How create correct collection to save data?


Solution

  • It sounds like you haven't read https://github.com/Readify/Neo4jClient/wiki/cypher or https://github.com/Readify/Neo4jClient/wiki/cypher-examples. You really should start there. It helps to read the manual. I don't just write it for fun.

    For your specific scenario, let's start with your Cypher query:

    MATCH (a:User)-[r]->(b:Phone)
    RETURN a, collect(b)
    

    Now, we convert that to fluent Cypher:

    var results = client.Cypher
        .Match("(a:User)-[r]->(b:Phone)")
        .Return((a, b) => new {
            User = a.As<User>(),
            Phones = b.CollectAs<Phone>()
        })
        .Results;
    

    You can then use this rather easily:

    foreach (var result in results)
        Console.WriteLine("{0} has {1} phone numbers", result.User.Name, result.Phones.Count());
    

    You will need classes like these, that match your node property model:

    public class User
    {
        public string Name { get; set; }
        public string Surname { get; set; }
    }
    
    public class Phone
    {
        public string number { get; set; }
        public string type_of_number { get; set; }
    }
    

    Again, please go and read https://github.com/Readify/Neo4jClient/wiki/cypher and https://github.com/Readify/Neo4jClient/wiki/cypher-examples.