2016-07-15 49 views
1

我有一个表comments,这就是这个样子,加了一些样机的内容,以及:显示注释,如Gmail评论

+------------+---------+----------+-------------------+------------------------------------+---------------------------+ 
| comment_id | user_id | post_id | comment_parent_id |   comment_content   | comment_creation_datetime | 
+------------+---------+----------+-------------------+------------------------------------+---------------------------+ 
|   26 |  1 | 16329 |     0 | Första        | 2016-01-24 10:42:49  | 
|   27 |  1 | 16329 |    26 | Svar till första     | 2016-01-24 10:42:55  | 
|   28 |  1 | 16329 |    26 | Andra svar till förta    | 2016-01-24 10:43:06  | 
|   29 |  1 | 16329 |    28 | Svar till "andra svar till första" | 2016-01-24 10:43:23  | 
+------------+---------+----------+-------------------+------------------------------------+---------------------------+ 

我试着显示评论reddit的风格,这样的形象:

enter image description here

连我自己都不知道如何开始从哪里开始... 我试着用搜索引擎找到的文章这关系到我的要求,但我没有找到它。 任何帮助...... 我张贴在下面的评论形象,因为我没有名声点后的图像谢谢

+0

@fubo:如何在层次结构中显示评论 – user6503334

回答

1

假设你有一个类评论

public class Comment 
{ 
    public int comment_id { get; set; } 
    public int user_id { get; set; } 
    public int post_id { get; set; } 
    public int comment_parent_id { get; set; } 
    public string comment_content { get; set; } 
    public DateTime comment_creation_datetime { get; set; } 

    public Comment(int comment_id, int user_id, int post_id, int comment_parent_id, string comment_content, DateTime comment_creation_datetime) 
    { 
     this.comment_id = comment_id; 
     this.user_id = user_id; 
     this.post_id = post_id; 
     this.comment_parent_id = comment_parent_id; 
     this.comment_content = comment_content; 
     this.comment_creation_datetime = comment_creation_datetime; 
    } 

    public override string ToString() 
    { 
     return string.Format("{0} {1} {2}", this.comment_id, this.comment_content, this.comment_creation_datetime); 
    } 
} 

充满以下样本值

List<Comment> cList = new List<Comment>(); 
cList.Add(new Comment(26, 1, 16329, 0, "Första ", new DateTime(2016, 01, 24, 10, 42, 49))); 
cList.Add(new Comment(27, 1, 16329, 26, "Svar till första", new DateTime(2016, 01, 24, 10, 42, 55))); 
cList.Add(new Comment(28, 1, 16329, 26, "Andra svar till förta", new DateTime(2016, 01, 24, 10, 43, 06))); 
cList.Add(new Comment(29, 1, 16329, 28, "Svar till andra svar till första", new DateTime(2016, 01, 24, 10, 43, 23))); 

你需要一个方法,以便对项目进行排序

public static IEnumerable<Comment> Sort(List<Comment> SortItemsList, int parentID = 0) 
{ 
    foreach (var item in SortItemsList.Where(x => x.comment_parent_id == parentID).OrderBy(x => x.comment_id).ToList()) 
    { 
     yield return item; 
     foreach (var child in Sort(SortItemsList, item.comment_id)) 
     { 
      yield return child; 
     } 
    } 
} 

和一个以确定每个项目的层次结构的深度

public static int GetDepth(List<Comment> cList, Comment cItem) 
{ 
    if (cItem.comment_parent_id == 0) 
    { 
     return 0; 
    } 
    else 
    { 
     return GetDepth(cList, cList.Where(x => x.comment_id == cItem.comment_parent_id).Single()) + 1; 
    } 
} 

实施例:

foreach (Comment cItem in Sort(cList)) 
{ 
    Console.WriteLine(GetDepth(cList, cItem) + " | " + cItem.ToString()); 
} 

另一方法将是像here descriped评论的嵌套处理。