[英]Get hierarchical Data From Neo4j To C# Object Structure using Neo4jClient
我遇到了一種情況,需要使用neo4jClient將分層對象圖從Neo4j加載到C#中
我在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; }
}
這是我的圖:
現在,想像一個需要使用父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();
}
密碼將是這樣的:
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
但是問題是我不知道如何將結果加載到post C#類中的節點及其父級的層次結構中。
密碼結果是我在db上測試查詢時要查找的內容。 但是一切都被C#對象弄亂了。
這是neo4jDB中的查詢結果:
如果您的Post班有多個家長職位,那么我認為您應該更改
public Post Parent { get; set; }
像
public IEnumerable<Post> Parent { get; set; }
然后,您可以將密碼結果中的所有父節點映射到此屬性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.