如何在 C# 中构建线程评论系统? 帮助
我正在为我的网站构建线程评论系统,但遇到了问题...
我有一个从数据库中提取的列表,其中包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
鉴于您使用的是 Count() 扩展方法而不是 Count 属性(这本身效率有点低;不过,使用 foreach 会更好),您可能正在使用 .NET 3.5。
我认为我没有完全理解您的方案 - 例如,您图中带有 threadID=4 的注释位于第一个 threadID=3 元素而不是第二个元素下,有什么可说的?
在不了解您所追求的细节的情况下,一般来说,我会考虑使用注释数据结构:
鉴于此,如果您担心的是缩进级别,那么计算出缩进级别将相当容易。 如果这听起来有用,我可以详细说明 - 如果没有,请澄清问题。
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:
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.
感谢大家的帮助。 我很感激。
不过我确实找到了一个人写的东西,他为它写了所有的东西。
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
您需要一个递归函数,并且根据您遍历列表的方式,存储 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.
这可能有效:
This might work: