我正在尝试找到一种最有效的方法来显示从数据库模式到c#对象的一组嵌套注释,我可以将其转换为序列化的JSON对象。
我的架构如下:
从屏幕截图中可以看到,有一个更新,其中包含父问题ID(此处不相关)和注释。还有一个ParentUpdateID,它允许表存储每个回复的嵌套回复,依此类推。
我在c#中创建了一个类,我希望转换来自我的Entity Framework调用的结果;
/// <summary>
/// The resource view model.
/// </summary>
public class Comment
{
/// <summary>
/// Gets or sets the id.
/// </summary>
public Guid Id { get; set; }
/// <summary>
/// Gets or sets the body.
/// </summary>
public string Body { get; set; }
/// <summary>
/// Gets or sets the modified by.
/// </summary>
public string ModifiedBy { get; set; }
/// <summary>
/// Gets or sets the modified date.
/// </summary>
public DateTime ModifiedDate { get; set; }
/// <summary>
/// Gets or sets the created by.
/// </summary>
public string CreatedBy { get; set; }
/// <summary>
/// Gets or sets the created date.
/// </summary>
public DateTime CreatedDate { get; set; }
/// <summary>
/// Gets or sets the parent comment id.
/// </summary>
public Guid? ParentCommentId { get; set; }
/// <summary>
/// Gets or sets the children.
/// </summary>
public List<Comment> Children { get; set; }
}
以下是我从数据库中检索评论/更新列表的方法。
List<ClientIssuesUpdate> updates = this.db.ClientIssuesUpdates.Where(i => i.IssueId == issueId).ToList();
是否有一种聪明的方法可以通过循环或使用lync来排序显示最近的所有评论,但随后按日期顺序显示每个评论子项等等。 (例如,所有没有ParentUpdateId的评论都是评论级别)
任何指导都会有所帮助,因此我可以为将来学习。
非常感谢
答案 0 :(得分:0)
这应按创建日期排序。
根据您的注释子项嵌套的深度,您必须添加更多循环。
List<ClientIssuesUpdate> updates = this.db.ClientIssuesUpdates.Where(i => i.IssueId == issueId).ToList().OrderByDescending(p => p.CreatedDate);
foreach (var comment in updates.Where(comment => comment.Children != null))
{
// order the children
comment.Children = new List<Comment>(comment.Children.OrderByDescending(c => c.CreatedDate));
}
*无法记住您是应该使用OrderBy<>
还是OrderByDescending<>