使用 Neo4jClient 从 Neo4j 获取分层数据到 C# 对象结构
Get hierarchical Data From Neo4j To C# Object Structure using Neo4jClient
我遇到了需要使用 neo4jClient 将分层对象图从 Neo4j 加载到 C# 的情况
我在 C# 中有一个 post Class 作为休闲:
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
加载图形的所有父及其所有相关属性的方法
我想出了 Produce 需要 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
但问题是我不知道如何在 post C# class.
中将结果加载到节点及其父节点的层次结构中
当我在 db 上测试查询时,密码结果就是我正在寻找的结果。但是 C# 对象中的一切都搞砸了。
这里是neo4jDB中的查询结果:
如果您的 Post class 有多个家长帖子,那么我认为您应该更改
public Post Parent { get; set; }
类似于
public IEnumerable<Post> Parent { get; set; }
然后你可以将密码结果中的所有父节点映射到这个属性
我遇到了需要使用 neo4jClient 将分层对象图从 Neo4j 加载到 C# 的情况
我在 C# 中有一个 post Class 作为休闲:
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
加载图形的所有父及其所有相关属性的方法我想出了 Produce 需要 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
但问题是我不知道如何在 post C# class.
中将结果加载到节点及其父节点的层次结构中当我在 db 上测试查询时,密码结果就是我正在寻找的结果。但是 C# 对象中的一切都搞砸了。
这里是neo4jDB中的查询结果:
如果您的 Post class 有多个家长帖子,那么我认为您应该更改
public Post Parent { get; set; }
类似于
public IEnumerable<Post> Parent { get; set; }
然后你可以将密码结果中的所有父节点映射到这个属性