使用Neo4jClient从Neo4j获取分层数据到C#对象结构

时间:2015-01-26 17:23:37

标签: c# neo4j neo4jclient

我遇到了需要使用neo4jClient将Neo4j中的分层对象Graph加载到C#中的情况

我在C#中有一个帖子类作为休闲:

public class Post : BaseNode
{
    public Post Parent { get; set; }
    public float Id { get; set; }
    public string Text { get; set; }
    public ICollection<HashTag> HashTags { get; set; }
    public ICollection<IKeyCommand> KeyTags { get; set; }
    public ICollection<NeoDateTime> DueDates { get; set; }
    public IdentityUser Author { get; set; }
    public ICollection<IdentityUser> MentionedUsers { get; set; }
    public ICollection<Team> MentionedTeam { get; set; }
    public ICollection<PostAttachment> Attachments { get; set; }
    public string Priority { get; set; }

}

这是我的图表: ThePost Graph

现在想象一个方法需要使用父ID

加载图表的所有父项及其所有相关属性

我已经提出了这个产生所需Cypher的C#代码:

public IList<Post> LoadParents(int id)
    {
        var matchStringTemplate = "(post:Post {{Id:{0}}})-[r:HAS_Parent*]->(parents:Post)";
        var matchString = string.Format(matchStringTemplate, id);

        var q = new CypherFluentQuery(_graphClient) as ICypherFluentQuery;
        q = q.Match(string.Format(matchStringTemplate, id));

        q = q.OptionalMatch("(post:Post)-[mentionedUsersRelation:HAS_MentionedUsers]->(allAssignees:User)");
        q = q.OptionalMatch("(post:Post)-[authorRelation:HAS_Author]->(author:User)");
        q = q.OptionalMatch("(post:Post)-[hashtagRElation:HAS_HashTags]->(hashtags:HashTag)");
        q = q.OptionalMatch("(post:Post)-[HasAttachmentRelation:HAS_Attachments]->(attachment:PostAttachment)");

        q = q.OptionalMatch("(parents:Post)-[pmentionedUsersRelation:HAS_MentionedUsers]->(pallAssignees:User)");
        q = q.OptionalMatch("(parents:Post)-[pauthorRelation:HAS_Author]->(pauthor:User)");
        q = q.OptionalMatch("(parents:Post)-[phashtagRElation:HAS_HashTags]->(phashtags:HashTag)");
        q = q.OptionalMatch("(parents:Post)-[pHasAttachmentRelation:HAS_Attachments]->(pattachment:PostAttachment)");

        var rq = q.Return((post, parents,  allAssignees, author, hashtags, attachment,

            pallAssignees, pauthor, phashtags, pattachment) => new
        {
            Post = post.As<Post>(),
            Parent = parents.As<Post>(),
            Author = author.As<IdentityUser>(),
            MentionedUsers = allAssignees.CollectAsDistinct<IdentityUser>(),
            Attachments = attachment.CollectAsDistinct<PostAttachment>(),
            HashTags = hashtags.CollectAsDistinct<HashTag>()
        })
        .OrderByDescending("post.CreationDate");

        var result = rq.Results;
        var results = result.Select(p => new Post()
        {
            Id = p.Post.Id,

            Text = p.Post.Text,
            CreationDate = p.Post.CreationDate,
            Author = p.Author,
            MentionedUsers = p.MentionedUsers.Select(m => m.Data).ToArray(),
            HashTags = p.HashTags.Select(h => h.Data).ToArray(),
            Attachments = p.Attachments.Select(a => a.Data).ToArray()
        });
        return results.ToList();
    }
Cypher会是这样的:

 MATCH (post:Post {Id:9601})-[r:HAS_Parent*]->(parents:Post)

OPTIONAL MATCH (post:Post)-[mentionedUsersRelation:HAS_MentionedUsers]->(allAssignees:User)
OPTIONAL MATCH (post:Post)-[authorRelation:HAS_Author]->(author:User)
OPTIONAL MATCH (post:Post)-[hashtagRElation:HAS_HashTags]->(hashtags:HashTag)
OPTIONAL MATCH (post:Post)-[HasAttachmentRelation:HAS_Attachments]->(attachment:PostAttachment)
OPTIONAL MATCH (parents:Post)-[pmentionedUsersRelation:HAS_MentionedUsers]->(pallAssignees:User)

OPTIONAL MATCH (parents:Post)-[pauthorRelation:HAS_Author]->(pauthor:User)
OPTIONAL MATCH (parents:Post)-[phashtagRElation:HAS_HashTags]->(phashtags:HashTag)
OPTIONAL MATCH (parents:Post)-[pHasAttachmentRelation:HAS_Attachments]->(pattachment:PostAttachment)
RETURN post AS Post, collect(distinct parents) AS Parent, author AS Author, collect(distinct allAssignees) AS MentionedUsers, collect(distinct attachment) AS Attachments, collect(distinct hashtags) AS HashTags
ORDER BY post.CreationDate DESC

但问题是我不知道如何将结果加载到节点的层次结构strutuer及其后C#类中的父节点。

当我在db上测试查询时,cypher结果是我正在寻找的。但是一切都搞砸了C#对象。

这是neo4jDB中的查询结果:

Post Parents

1 个答案:

答案 0 :(得分:1)

如果您的Post类有多个父母帖子,那么我认为您应该更改

public Post Parent { get; set; }

类似

public IEnumerable<Post> Parent { get; set; }

然后,您可以将cypher结果中的所有父节点映射到此属性

相关问题