Search code examples
neo4jneo4jclient

Can't get property from query in Neo4jClient C#


This is first time I try to use Neo4jClient and have no experiences. I expected my program can print out Name of people with specific relationship has assigned in Neo4j. I've got very simple code like:

    using Neo4jClient;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Neo4J.NET_Labs
    {
        class Program
        {
            static void Main(string[] args)
            {
                var client = new GraphClient(new Uri("http://localhost:7474/db/data"));
                client.Connect();

                var query = client
                    .Cypher
                    .Match("(n)-[:LOVE]-(lover)")
                    .Return(lover => lover.As<Person>())
                                    ;
                int count = 0;
                foreach (var result in query.Results)
                {
                    count++;
                    Console.WriteLine("People {0} count {1}", result.name, count);
                }

        //Stop to show result
        Console.ReadLine();
    }
}
public class Person
{
    public string name;
}

}

Result of snip:

    People  count 1
    People  count 2

Result of Code

Could someone pls tell me how to get properties form query.Result.


Solution

  • It is because of the definition of the 'Person' class, JSON.Net is unable to deserialize to members, and requires properties. You can test this by running the below code:

    public class Person { public string Name { get;set;} }
    public class PersonWithMembers { public string Name; }
    

    Basically identical classes, one with a property, one with a member.

    //Stick this in the Main method
    var pA = new Person {Name = "PersonA", Twitter = "tA"};
    var pB = new Person {Name = "PersonB", Twitter = "tB"};
    
    gc.Cypher.Create("(p:Person {person})").WithParam("person", pA).ExecuteWithoutResults();
    gc.Cypher.Create("(p:Person {person})").WithParam("person", pB).ExecuteWithoutResults();
    
    Console.WriteLine("Members:");
    var membersQuery = gc.Cypher
        .Match("(p:Person)")
        .Return(p => p.As<PersonWithMembers>());
    
    foreach (var p in membersQuery.Results)
        Console.WriteLine(p.Name);
    
    Console.WriteLine("Properties:");
    var propertiesQuery = gc.Cypher
        .Match("(p:Person)")
        .Return(p => p.As<Person>());
    
    foreach (var p in propertiesQuery.Results)
        Console.WriteLine(p.Name);
    

    You should get the output:

    Members:
    
    
    Properties:
    PersonA
    PersonB