使用cypher neo4j创建一个链表

时间:2015-06-23 16:36:46

标签: neo4j cypher neo4jclient

有没有办法在一个交易中在密码中创建链表?
我试过ForEach with Match但是根据neo4jClien,不可能在ForEach中设置Match。 我的方法:

public static void save(List<Post> nodes)
{
    var gclient = graphdb.getConnection();
    var create1 = gclient.Cypher.Create("(p:Post {nodes})");
    var match = gclient.Cypher.Match("((t)-[r:lastPost]->(last))");
    var create3 = gclient.Cypher.Create("t-[:lastPost]->p, p-[:next]->last");
    var delete = gclient.Cypher.Delete("r");

    string query = create1.Query.QueryText + " " + match.Query.QueryText + " "
                       + create3.Query.QueryText + " " + delete.Query.QueryText;

    gclient.Cypher
           .Match("(t:Tmp)")
           .WithParam("nodes", nodes)
           .ForEach("(newPost in {nodes} | " + query + ")")
           .ExecuteWithoutResults();
}

提前致谢。

1 个答案:

答案 0 :(得分:3)

这应该可以解决问题。

    static Neo4jClient.Cypher.ICypherFluentQuery addnode<T>(Neo4jClient.Cypher.ICypherFluentQuery q, IList<T> items, int idx, string label)
    {
        string sq = string.Format("({0}:{1} {{{2}}})", "c" + idx, label, "a" + idx);
        q = q.Create(sq).WithParam("a" + idx, items[idx]);
        return q;
    }
    static Neo4jClient.Cypher.ICypherFluentQuery addlink<T>(Neo4jClient.Cypher.ICypherFluentQuery q, int idx1, int idx2)
    {
        string sq = string.Format("{0}-[:LINKEDTO]->{1}", "c" + idx1, "c" + idx2);
        q = q.Create(sq);
        return q;
    }

    public static void Sample<T>(List<T> items, GraphClient client)
    {
        Neo4jClient.Cypher.ICypherFluentQuery q = client.Connection.Cypher;
        for (int i = 1; i < items.Count; i++)
        {
            q = addnode<T>(q, items, i-1, "MYITEM");
            if(i>1)
                q = addlink<T>(q, i-2, i-1);

        }
        q.ExecuteWithoutResults();
    }