2009-01-14 38 views
1

林建立螺纹评论系统为我的网站,我遇到了一个问题...如何在C#中构建一个线程评论系统?帮助

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

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

例如:

--- 1。 Blah

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

----------- 3。等等等等 - >的parentID = 2

-------------- 4.等等等等 - >的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]); 
      } 
     } 
    } 

这似乎中途整理,但不是真正的...的线程ID当然是它得到多远种植权。

+0

参见[http://stackoverflow.com/questions/321680/building-a-database-driven-menu-with-asp-net-jquery-和口鱼#321773](http://stackoverflow.com/questions/321680/building-a-database-driven-menu-with-asp-net-jquery-and-suckerfish#321773)。 – RedFilter 2009-01-14 05:51:58

回答

0

您需要一个递归函数,并且根据它看起来像遍历列表的方式,最好存储ID和ChildID(而不是父ID)。这种方式递归函数可以在ChildID == null时结束遍历。

0

鉴于您使用的是Count()扩展方法而不是Count属性(本身略微没有效率;使用foreach会更好,尽管如此),您大概会使用.NET 3.5。

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

不知道在很多的你追求的详细信息的方式,一般我会考虑用注释数据结构:

  • CommentID:本实体
  • RootID的标识:该线程的根元素的ID(这样你就可以获取的容易线程所有评论)
  • PARENTID:如果它是根元素的父此评论的CommentID,或空
  • 时间戳:还是其他什么东西这将允许一位家长的孩子评论如此适当的。

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

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