1

我正在为我的一个网站构建一个线程评论系统,我遇到了一个问题......

我有一个从具有 ID 字段和父 ID 字段的数据库中提取的列表。父 ID 字段可以为空,但 ID 字段永远不会为空。

由于这将是一个线程评论系统,我将列表组织到 ID 位于顶部的位置,但如果存在父 ID,则它将插入到 ID 下。然后这也可以无限进行。所以第二级现在也有一个 ID,我想在它下面插入任何具有该 ID 的父 ID 的项目。

例如:

---1。废话

--------2。Blah Blah -> ParentID=1

------------ 3。Blah Blah -> parentID=2

-------------- 4. Blah Blah ->parentID=3

----------- 3.Blah Blah -> parentID=2

--------2。Blah Blah -> parentID=1

我认为你说对了。

所以这就是我到目前为止所拥有的......

List<comment> finalList = new List<comment>();
    for (int i = 0; i < getComments.Count(); i++)
    {
        string item = getComments[i].parentComment;
        getComments[i].threadID = 1;
        finalList.Add(getComments[i]);
        for (int ii = 0; ii < getComments.Count(); ii++)
        {
            if (getComments[ii].commentID == item)
            {
                getComments[ii].threadID = 2;
                finalList.Add(getComments[i]);
            }
        }
    }

它似乎对它进行了一半排序,但不是真正的...... ThreadID 当然是它被种植到右边的距离。

4

4 回答 4

1

鉴于您使用的是 Count() 扩展方法而不是 Count 属性(这本身就有点效率低下;尽管开始使用 foreach 会更好),您可能正在使用 .NET 3.5。

我不认为我完全理解您的方案 - 例如,您的图表中带有 threadID=4 的注释在第一个 threadID=3 元素而不是第二个元素下有什么说法?

在不了解您所追求的详细信息的情况下,通常我会考虑使用以下注释数据结构:

  • CommentID:这个实体的ID
  • RootID:线程的根元素的 ID(因此您可以轻松获取线程的所有评论)
  • ParentID:此评论的父级的 CommentID,如果是根元素,则为 null
  • 时间戳:或其他允许对一个父级中的子评论进行适当排序的东西。

鉴于此,如果这是您所关心的,那么计算缩进级别将相当容易。如果这听起来有用,我可以详细说明 - 如果没有,请澄清问题。

于 2009-01-14T07:10:22.463 回答
1

谢谢大家的帮助。我很感激。

不过,我确实找到了一个完全为它写了一切的人的东西。

http://www.scip.be/index.php?Page=ArticlesNET23

http://www.scip.be/index.php?Page=ArticlesNET09

http://www.scip.be/index.php?Page=ArticlesNET18

于 2009-01-14T15:42:59.650 回答
0

您需要一个递归函数,并且根据您遍历列表的样子,存储 ID 和 ChildID(而不是父 ID)可能会更好。这样递归函数可以在 ChildID == null 时结束遍历。

于 2009-01-14T05:46:31.993 回答
0

这可能有效:

class Program
    {
        static void Main(string[] args)
        {
            CommentCollection collection=new CommentCollection();
            Comment c1=new Comment("Blah",1,0,collection);
            Comment c2=new Comment("Blah blah",2,1,collection);
            Comment c3=new Comment("Blah blah", 3, 2, collection);
            Console.WriteLine(collection);
        }
    }
    [DebuggerDisplay("{id}-{parentId}: {text}")]
    class Comment:IEnumerable<Comment>
    {
        private readonly CommentCollection collection;
        private readonly int parentId;

        public Comment(string text, int id, int parentId, CommentCollection collection)
        {
            Id = id;
            this.parentId = parentId;
            collection.Add(this);
            this.collection = collection;
            this.text = text;
        }
        public Comment Parent
        {
            get
            {
                if (parent == null)
                {
                    parent = parentId == 0 ? null : collection[parentId];
                }
                return parent;
            }
        }

        private Comment parent;
        private readonly string text;
        public int Id{ get; private set;}
        public IEnumerator<Comment> GetEnumerator()
        {
            return collection.Where(c => c.Parent == this).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        public int Level
        {
            get { return Parent == null ? 0 : Parent.Level + 1; }
        }
        public override string ToString()
        {
            return Parent == null ? text : Parent + " > " + text;
        }
        public string ToString(bool tree)
        {
            if (!tree)
            {
                return ToString();
            }
            else
            {
                StringBuilder output = new StringBuilder();
                output.AppendLine(new string(' ', Level) + ToString(false));
                foreach (Comment comment in this)
                {
                    output.AppendLine(comment.ToString(true));
                }
                return output.ToString();
            }
        }
    }
    class CommentCollection:IEnumerable<Comment>
    {
        public void Add(Comment comment)
        {
            comments.Add(comment.Id,comment);
        }
        public Comment this[int id]
        {
            get { return comments[id]; }
        }
        private readonly Dictionary<int,Comment> comments=new Dictionary<int, Comment>();

        public IEnumerator<Comment> GetEnumerator()
        {
            return comments.Select(p => p.Value).GetEnumerator();
        }

        public IEnumerable<Comment> GetTopLevel()
        {
            return comments.Where(c => c.Value.Parent == null).
                Select(c => c.Value);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        public override string ToString()
        {
            StringBuilder output=new StringBuilder();
            foreach (Comment comment in GetTopLevel())
            {
                output.AppendLine(comment.ToString(true));
            }
            return output.ToString();
        }
    }
于 2009-01-14T12:40:50.407 回答