我正在为我的一个网站构建一个线程评论系统,我遇到了一个问题......
我有一个从具有 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 当然是它被种植到右边的距离。