Neo4j Cypher 客户端创建动态对象

Neo4j Cypher Client Create Dynamic Objects

目前我必须指定在 return 期间需要什么 class,有没有办法获取所有属性并创建一个 dynamic/anonymous 对象。

public IEnumerable<Node> GetNodes()
    {
        Console.WriteLine($"Retrieving All Nodes from DB...");
        try
        {
            return graphClient.Cypher
               .Match($"(n:Node)")
               .Return(t => new Node
               {
                   Latitude = Return.As<double>("n.lat"),
                   Longitude = Return.As<double>("n.lon"),
                   Id = Return.As<string>("n.id"),
                   ValA = Return.As<long>("n.valA")
               }).Results;
        }
        catch (Exception ex)
        {
            string error = $"ERROR (Get Node): {ex.ToString()}";
            Console.WriteLine(error);
        }

        return null;
    }

你可以使用我为这个问题写的解决方案:

我会争辩 - 如果这是你真正想做的 - 你最好使用官方驱动程序 (Neo4j-Driver) 或者实际上只是使用标准的 REST 客户端并直接连接 - 但是 - 上面的答案应该让你去你想去的地方!

尝试使用 Neo4J official C# driver 和 JSON.NET,例如:

public static void Main()
{
    string neo4jHostname = string.Empty, neo4jUsername = string.Empty, neo4jPassword = string.Empty;
    IDriver driver = GraphDatabase.Driver(neo4jHostname, AuthTokens.Basic(neo4jUsername, neo4jPassword));

    using (ISession session = driver.Session())
    {
        string query = "MATCH (n) RETURN n";
        IStatementResult resultCursor = session.Run(query);
        List<IRecord> res = resultCursor.ToList();
        string values = JsonConvert.SerializeObject(res.Select(x => x.Values), Formatting.Indented);
        List<JObject> nodes = JsonConvert.DeserializeObject<List<JObject>>(values);
    }
}