我一直在ASP.NET中處理頁面。這個頁面的行爲完全像一個論壇。用戶可以回覆條目。答覆表示爲ItemReply。當用戶回覆一個項目時,ParentID被設置爲用戶正在響應的ItemReply的ID。C#中元素層次結構中的排序問題
public class ItemReply
{
public Guid ID { get; set; }
public Guid? ParentID { get; set; }
public int Level { get; set; }
public string Remarks { get; set; }
public DateTime CreatedOn { get; set; }
public string CreatedBy { get; set; }
public ItemReply(DataRow row)
{
ID = GetGuidFromRow(row, "ID", 0);
ParentID = GetGuidFromRow(row, "ParentID", null);
Remarks = GetStringFromRow(row, "Remarks", string.Empty);
Level = 1;
CreatedOn = GetDateTimeFromRow(row, "CreatedOn", DateTime.UtcNow);
CreatedBy = GetStringFromRow(row, "CreatedBy", string.Empty);
}
}
public class ItemComparer : IComparer<ItemReply>
{
IDictionary<Guid, ItemReply> itemLookup;
public ReplyComparer(IEnumerable<ItemReply> list)
{
itemLookup = list.ToDictionary(item => item.ID);
foreach (var item in list)
SetLevel(item);
}
public int SetLevel(ItemReplyitem)
{
if (item.Level == 0 && item.ParentID.HasValue)
item.Level = 1 + itemLookup[item.ParentID.Value].Level;
return item.Level;
}
public int Compare(ItemReply x, ItemReply y)
{
// see if x is a child of y
while (x.Level > y.Level)
{
if (x.ParentID == y.ID)
return 1;
x = itemLookup[x.ParentID.Value];
}
// see if y is a child of x
while (y.Level > x.Level)
{
if (y.ParentID == x.ID)
return -1;
y = itemLookup[y.ParentID.Value];
}
// x and y are not parent-child, so find common ancestor
while (x.ParentID != y.ParentID)
{
if (x.ParentID.HasValue)
x = itemLookup[x.ParentID.Value];
if (y.ParentID.HasValue)
y = itemLookup[y.ParentID.Value];
}
// compare createDate of children of common ancestor
return x.CreatedOn.CompareTo(y.CreatedOn);
}
}
此代碼基本上是執行經:
List<ItemReply> replies = GetRepliesFromDataSource();
replies.Sort(new ReplyComparer(replies));
的順序和層次結構似乎正常工作。但是,SetLevel方法無法正常工作。 SetLevel的用途主要用於確定從UI角度縮進回覆的距離。爲了演示,想象下面的層次結構:
- Root
-- Child 1
--- Grandchild A
--- Grandchild B
-- Child 2
--- Grandchild C
由於某種原因,我的所有ItemReply元素的級別都是1.我做錯了什麼?
謝謝!
'SetLevel()'方法代碼在哪裏? – sll