以嵌入方式显示评论,例如Gmail评论

时间:2016-07-15 11:08:03

标签: c# asp.net comments

我有一个表comments,看起来像这样,还添加了一些模型内容:

+------------+---------+----------+-------------------+------------------------------------+---------------------------+
| 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

即使我不明白如何开始从哪里开始...... 我试着谷歌搜索找到与我的要求相关的文章,但我没有找到它。 任何帮助...... 我在下面的评论中张贴图片,因为我没有声望点发布图片谢谢

1 个答案:

答案 0 :(得分: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描述的评论进行嵌套处理。