如何在 C# 中构建线程评论系统? 帮助

发布于 2024-07-12 07:08:23 字数 1137 浏览 6 评论 0原文

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

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

由于这将是一个线程评论系统,因此我将列表组织到 ID 位于顶部的位置,但如果存在父 ID,则它将插入到该 ID 下。 那么这也可以无限持续下去。 因此,第二级现在也有一个 ID,并且我想在其下方插入具有该 ID 的父 ID 的任何项目。

例如:

---1。 废话

--------2。 巴拉巴拉-> ParentID=1

----------3. 巴拉巴拉-> ParentID=2

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

----------- 3.Blah Blah -> 父 ID=2

--------2. 巴拉巴拉-> 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 当然是它被植入到右侧的距离。

Im building a Threaded Comment System for a website of mine and I ran into a problem...

I have a list PULLED FROM A DATABASE that has a ID field and a Parent ID Field. The parent ID field can be null, but the ID field will NEVER be null.

Since this will be a threaded comment system, I organize the list to where the ID is the top one, but if a parent ID exists, then it would be inserted under the ID. Then this can go on for infinity also. So the second level now also has an ID and and I want to insert any item with a parent ID of that ID under it.

For example:

---1. Blah

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

I think you get the point.

So here is what I have so far...

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]);
            }
        }
    }

It seems to sort it half way, but not truly... The ThreadID is of course how far it gets planted to the right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

dawn曙光 2024-07-19 07:08:23

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

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

在不了解您所追求的细节的情况下,一般来说,我会考虑使用注释数据结构:

  • CommentID:该实体的 ID
  • RootID:线程的根元素的 ID(因此您可以获取一个线程的所有评论)
  • ParentID:此评论的父级的 CommentID,如果它是根元素,则为 null
  • Timestamp:或者允许对一个父级中的子级评论进行适当排序的其他内容。

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

Given that you're using the Count() extension method instead of the Count property (which is a slight inefficiency in itself; using foreach would be better to start with though) you're presumably using .NET 3.5.

I don't think I fully understand your scheme - for instance, what is there to say that the comment with threadID=4 in your diagram goes under the first threadID=3 element instead of the second?

Without knowing much in the way of details of what you're after, in general I'd consider a commenting data structure with:

  • CommentID: the ID of this entity
  • RootID: the ID of the root element for the thread (so you can fetch all comments for a thread easily)
  • ParentID: the CommentID of the parent for this comment, or null if it's the root element
  • Timestamp: Or something else which would allow the child comments within one parent to be sorted appropriately.

Given that, it would be fairly easy to work out the indentation level, if that's what you're concerned about. If that sounds useful, I can go into more details - if not, please clarify the question.

℡寂寞咖啡 2024-07-19 07:08:23

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

不过我确实找到了一个人写的东西,他为它写了所有的东西。

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

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

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

Thanks all for your help guys. I do appreciate it.

I did though, find something by a guy that wrote absolutely everything for it.

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

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

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

凡尘雨 2024-07-19 07:08:23

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

You need a recursive function, and, based on how it looks like you're traversing the list, it would probably be better to store ID and ChildID (rather than parent ID). This way the recursive function can end a traversal when ChildID == null.

自由如风 2024-07-19 07:08:23

这可能有效:

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();
        }
    }

This might work:

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();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文