C#에서 스레드 댓글 시스템을 구축하는 방법? 돕다
-
22-07-2019 - |
문제
내 웹 사이트를위한 스레드 코멘트 시스템을 구축하고 문제가 발생했습니다 ...
ID 필드와 상위 ID 필드가있는 데이터베이스에서 목록을 가져 왔습니다. 상위 ID 필드는 null 일 수 있지만 ID 필드는 결코 null이 아닙니다.
이것은 스레드 주석 시스템이므로 ID가 맨 위의 위치로 목록을 구성하지만 상위 ID가 존재하면 ID에 삽입됩니다. 그러면 이것은 무한대에도 계속 될 수 있습니다. 따라서 두 번째 레벨에는 이제 ID가 있으며 해당 ID의 상위 ID가있는 항목을 삽입하려고합니다.
예를 들어:
---1. 블라
-------- 2. blah blah-> parentid = 1
-----------삼. 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]);
}
}
}
그것은 그것을 반쯤 분류하는 것처럼 보이지만 진정으로는 그렇지 않습니다.
해결책 2
도움을 주셔서 감사합니다. 감사합니다.
그래도 나는 그것을 위해 절대적으로 모든 것을 쓴 남자가 무언가를 찾았습니다.
http://www.scip.be/index.php?page=articlesnet23
다른 팁
카운트 속성 대신 COUNT () 확장 방법을 사용하는 경우 (Foreach를 사용하는 것이 더 나을 것입니다) 아마도 .NET 3.5를 사용하는 것으로 추정됩니다.
예를 들어, 당신의 계획을 완전히 이해하지 못한다고 생각합니다. 예를 들어, 다이어그램에서 threadid = 4가있는 주석이 두 번째 대신 첫 번째 threadid = 3 요소 아래에 있다고 말하는 것은 무엇입니까?
당신이 따른 내용에 대한 세부 사항을 많이 알지 못하고, 일반적으로 나는 다음과 같이 주석 데이터 구조를 고려할 것입니다.
- 의견 :이 엔티티의 ID
- rootid : 스레드의 루트 요소의 ID (따라서 스레드에 대한 모든 주석을 쉽게 가져올 수 있습니다)
- Parentid :이 의견에 대한 부모의 의견 또는 루트 요소 인 경우 Null
- 타임 스탬프 : 또는 한 부모 내의 자녀가 적절하게 정렬 할 수있는 다른 것.
그 점을 감안할 때, 그것이 당신이 걱정하는 것이라면, 들여 쓰기 수준을 해결하는 것은 상당히 쉬울 것입니다. 그것이 유용하게 들리면 더 자세한 내용을 살펴볼 수 있습니다. 그렇지 않은 경우 질문을 명확히하십시오.
재귀 기능이 필요하며, 목록을 가로 지르는 것처럼 보이는 방식에 따라 상위 ID가 아닌 ID와 childid를 저장하는 것이 좋습니다. 이런 식으로 재귀 함수는 childid == null 일 때 트래버스를 종료 할 수 있습니다.
이것은 작동 할 수 있습니다 :
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();
}
}