【问题标题】:Neo4j Driver C# Unwind a list of objectsNeo4j Driver C# 展开对象列表
【发布时间】:2020-04-16 13:53:20
【问题描述】:

我正在寻找如何将对象列表(在内存中)展开到 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();
        }

【问题讨论】:

    标签: c# neo4j neo4j-driver


    【解决方案1】:

    遗憾的是,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();
    }
    

    【讨论】:

      猜你喜欢
      • 2022-07-21
      • 1970-01-01
      • 2018-01-18
      • 2023-01-27
      • 1970-01-01
      • 2019-12-15
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      相关资源
      最近更新 更多