简体   繁体   English

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

[英]Get hierarchical Data From Neo4j To C# Object Structure using Neo4jClient

I have get in to a situation that need to Load a hierarchical object Graph from Neo4j into C# using neo4jClient 我遇到了一种情况,需要使用neo4jClient将分层对象图从Neo4j加载到C#中

I have a post Class in C# as fallow: 我在C#中有一个Post类作为休假:

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; }

}

and Here is my Graph : 这是我的图: 邮政图

Now Imagin a method wich need to load all the parent of a graph with all its related properties using parent Id 现在,想像一个需要使用父ID来加载图的所有父项及其所有相关属性的方法

I have Came up with this C# Code which Produce needed Cypher : 我已经想到了产生所需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();
    }

the Cypher would be something like this: 密码将是这样的:

 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

But the problem is I dont kow how to load the result into the hierarchy strutuer of node and its parent in the post C# class. 但是问题是我不知道如何将结果加载到post C#类中的节点及其父级的层次结构中。

the cypher result is what i am looking for when I test the query on db. 密码结果是我在db上测试查询时要查找的内容。 but everything get messed up in C# object. 但是一切都被C#对象弄乱了。

here is the query result in neo4jDB: 这是neo4jDB中的查询结果:

邮寄父母

If your Post class has multiple parents posts, then I think you should change 如果您的Post班有多个家长职位,那么我认为您应该更改

public Post Parent { get; set; }

to something like

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

Then you can map all parent nodes in cypher result to this property 然后,您可以将密码结果中的所有父节点映射到此属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM