Neo4j Driver C# 展开对象列表

Neo4j Driver C# Unwind a list of objects

我正在研究如何将对象列表(在内存中)展开到 Neo4j 4.0 中。以下是我之前使用 Neo4jClient nuget 的内容,但我不得不改用 Neo4j.Driver nuget。

Neo4jClient(旧)

graphClient.Cypher
    .Unwind(towns, "tp")
    .Merge("t:Town {Name: tp.Id})")
    .OnCreate()
    .Set("t = tp")
    .ExecuteWithoutResults();

Neo4j 驱动程序(目前已完成)

var session = driver.AsyncSession(o => o.WithDatabase("neo4j"));
        try
        {
            towns = towns.OrderBy(tt => tt.Id).ToList();
            foreach (var t in towns.Split(5000))
            {
                    Console.WriteLine($"Saving {t.Count:N0} of {town.Count:N0} Towns...");    
        **//STUCK HERE DOING UNWIND**  
            }
        }
        catch (Exception ex)
        {
            string error = $"ERROR (ADD TOWNS): {ex.ToString()}";           
            Console.WriteLine(error);
        }
        finally
        {
            await session.CloseAsync();
        }

遗憾的是,Neo4j 驱动程序非常简陋 - 您需要手动构建 Cypher 查询,在需要的地方引用参数,然后将这些参数传递到查询中。

var session = driver.AsyncSession(o => o.WithDatabase("neo4j"));
try
{
    towns = towns.OrderBy(tt => tt.Id).ToList();
    foreach (var t in towns.Split(5000))
    {
        Console.WriteLine($"Saving {t.Count:N0} of {town.Count:N0} Towns...");

        // Just return the town name - in your case, you'd MERGE or whatever, this is 
        // just an example
        var query = new Query("UNWIND {towns} AS town RETURN town", new Dictionary<string, object>
        {
            { "towns", towns }
        });

        var result = await session.RunAsync(query);
    }
}
catch (Exception ex)
{
    string error = $"ERROR (ADD TOWNS): {ex.ToString()}";
    Console.WriteLine(error);
}
finally
{
    await session.CloseAsync();
}