如何在C#中构建线程注释系统?帮助
我正在为我的一个网站建立一个线程评论系统,我遇到了一个问题 我从一个数据库中提取了一个列表,该数据库有一个ID字段和一个父ID字段。父ID字段可以为空,但ID字段永远不会为空 因为这将是一个线程注释系统,所以我将列表组织到ID位于顶部的位置,但如果存在父ID,则会将其插入到ID下。这样也可以无限延伸。第二层现在也有一个ID,我想在下面插入任何一个父ID为该ID的项 例如: ---一,。废话 --------二,。废话废话->父ID=1 -----------三,。废话废话->父ID=2 --------------四,。废话废话->父ID=3 -----------3.诸如此类->父ID=2 --------二,。废话废话->父ID=1 我想你明白了 这就是我到目前为止所拥有的如何在C#中构建线程注释系统?帮助,c#,list,nested-sets,threaded-comments,C#,List,Nested Sets,Threaded Comments,我正在为我的一个网站建立一个线程评论系统,我遇到了一个问题 我从一个数据库中提取了一个列表,该数据库有一个ID字段和一个父ID字段。父ID字段可以为空,但ID字段永远不会为空 因为这将是一个线程注释系统,所以我将列表组织到ID位于顶部的位置,但如果存在父ID,则会将其插入到ID下。这样也可以无限延伸。第二层现在也有一个ID,我想在下面插入任何一个父ID为该ID的项 例如: ---一,。废话 --------二,。废话废话->父ID=1 -----------三,。废话废话->父ID=2 ----
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]);
}
}
}
List finalList=new List();
for(int i=0;i
它似乎把它分类了一半,但不是真的。。。ThreadID当然是它向右放置的距离。您需要一个递归函数,并且,根据遍历列表的方式,存储ID和ChildID(而不是父ID)可能会更好。这样,当ChildID==null时,递归函数可以结束遍历。假设您使用的是Count()扩展方法而不是Count属性(这本身有点低效;但是使用foreach会更好),那么您可能使用的是.NET 3.5 我不认为我完全理解您的方案-例如,有什么可以说,您的图表中带有threadID=4的注释位于第一个threadID=3元素下,而不是第二个元素下 <>你不知道你所追求的细节,一般来说我会考虑评论数据结构:
- CommentID:此实体的ID
- RootID:线程的根元素的ID(因此可以轻松获取线程的所有注释)
- ParentID:此注释的父级的CommentID,如果是根元素,则为null
- Timestamp:或者其他允许对父级中的子注释进行适当排序的内容
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();
}
}
类程序
{
静态void Main(字符串[]参数)
{
CommentCollection集合=新建CommentCollection();
注释c1=新注释(“废话”,1,0,集合);
注释c2=新注释(“废话”,2,1,集合);
注释c3=新注释(“废话”,3,2,集合);
控制台写入线(集合);
}
}
[调试程序显示(“{id}-{parentId}:{text}”)]
类注释:IEnumerable
{
私人只读评论集;
私有只读int-parentId;
公共注释(字符串文本、int-id、int-parentId、CommentCollection)
{
Id=Id;
this.parentId=parentId;
收藏。添加(本);
this.collection=collection;
this.text=文本;
}
公开评论家长
{
得到
{
如果(父项==null)
{
parent=parentId==0?空:集合[parentId];
}
返回父母;
}
}
私人评论家长;
私有只读字符串文本;
public int Id{get;private set;}
公共IEnumerator GetEnumerator()
{
返回集合。其中(c=>c.Parent==this.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
公共整数级
{
获取{return Parent==null?0:Parent.Level+1;}
}
公共重写字符串ToString()
{
返回父项==null?文本:父项+“>”+文本;
}
公共字符串到字符串(布尔树)
{
如果(!树)
{
返回到字符串();
}
其他的
{
StringBuilder输出=新的StringBuilder();
AppendLine(新字符串(“”,Level)+ToString(false));
foreach(本节中的注释)
{
AppendLine(comment.ToString(true));
}
返回output.ToString();
}
}
}
类集合:IEnumerable
{
公共作废添加(注释)
{
comments.Add(comment.Id,comment);
}
公众评论此[int id]
{
获取{返回注释[id];}
}
私有只读词典注释=新词典();
公共IEnumerator GetEnumerator()
{
返回注释.Select(p=>p.Value).GetEnumerator();
}
公共IEnumerable GetToLevel()
{
返回注释。其中(c=>c.Value.Parent==null)。
选择(c=>c.Value);
}
IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
公共重写字符串ToString()
{
StringBuilder输出