2012-05-31 45 views
3

我正在使用C#语言。我的问题是,我不知道如何将检索到的分层结果集存储到我的对象中。如何使用检索的分层结果集创建对象?

这里是我的对象:

public class CategoryItem 
{ 
    public string Name { get; set; } 
    public int CategoryID { get; set; } 
    public int ParentID { get; set; } 
    public List<CategoryItem> SubCategory = new List<CategoryItem>(); 
    public List<CategoryItem> GetSubCategory() 
    { 
     return SubCategory; 
    } 
    public void AddSubCategory(CategoryItem ci) 
    { 
     SubCategory.Add(ci); 
    } 
    public void RemoveSubCategory(CategoryItem ci) 
    { 
     for (int i = 0; i < SubCategory.Count; i++) 
     { 
      if (SubCategory.ElementAt(i).CategoryID == ci.CategoryID) 
      { 
       SubCategory.RemoveAt(i); 
       break; 
      } 
     } 
    } 
} 

这里是我的样本中检索数据从MSSQL服务器设置

ID PrntID Title 
_______ _______  
1 0 Node1 
2 1 Node2 
3 1 Node3 
4 2 Node4 
5 2 Node5 
6 2 Node6 
7 3 Node7 
8 4 Node8 
9 4 Node9 
10 9 Node10 
,以供参考

树视图

Node 1 
-Node 2 
--Node 4 
---Node 8 
---Node 9 
----Node 10 
--Node 5 
--Node 6 
-Node 3 
--Node 7 

我的问题是如何我将这个结果存储到我的“CategoryItem对象”中。我没有任何线索需要为此使用迭代吗?特别是当节点是2级深的时候。 我想将它存储在这样一个这样的:

List<CategoryItem> items = new List<CategoryItem>(); 

这个我可以在“项目”对象中的每个对象挖掘,我可以访问使用GetSubCategory它的子类/儿童/儿童()方法我的班级。这可能吗?

+0

你为什么要使用一个类的一切 –

+0

因为我将用我的Web应用程序,我打算扔了“CategoryItem列表”我的看法,所以,我认为,层那里我可以遍历“CategoryItem列表”,并且这种方法非常通用,即使层次结构有多深也没有限制。 – chlkdst

回答

2

如果你知道在你的DataSet节点永远不会出现在它的父节点之前,你可以使用这段代码。在这里,您可以跟踪字典中已读取的项目,以便查找新读取节点的父项。如果您发现父项,则将新项目添加到其子项,否则它是第一级节点。

public static List<CategoryItem> LoadFromDataSet(DataSet aDS) 
    { 
     List<CategoryItem> result = new List<CategoryItem>(); 
     Dictionary<int, CategoryItem> alreadyRead = new Dictionary<int, CategoryItem>(); 
     foreach (DataRow aRow in aDS.Tables["YourTable"].Rows) 
     { 
      CategoryItem newItem = new CategoryItem(); 
      newItem.CategoryID = (int)aRow["ID"]; 
      newItem.ParentID = (int)aRow["PrntID"]; 
      newItem.Name = (string)aRow["Title"]; 
      alreadyRead[newItem.CategoryID] = newItem; 
      CategoryItem aParent; 
      if (alreadyRead.TryGetValue(newItem.ParentID, out aParent)) 
       aParent.AddSubCategory(newItem); 
      else 
       result.Add(newItem); 
     } 
     return result; 
    } 

如果我的假设是不正确的(即,它是可能的节点出现在其父前的数据集),您必须首先阅读所有的节点(并把它们在词典),然后通过相同的字典循环来建立结果。这样的事情:

public static List<CategoryItem> LoadFromDataSet(DataSet aDS) 
    { 
     List<CategoryItem> result = new List<CategoryItem>(); 
     Dictionary<int, CategoryItem> alreadyRead = new Dictionary<int, CategoryItem>(); 
     foreach (DataRow aRow in aDS.Tables["YourTable"].Rows) 
     { 
      CategoryItem newItem = new CategoryItem(); 
      newItem.CategoryID = (int)aRow["ID"]; 
      newItem.ParentID = (int)aRow["PrntID"]; 
      newItem.Name = (string)aRow["Title"]; 
      alreadyRead[newItem.CategoryID] = newItem; 
     } 
     foreach (CategoryItem newItem in alreadyRead.Values) 
     { 
      CategoryItem aParent; 
      if (alreadyRead.TryGetValue(newItem.ParentID, out aParent)) 
       aParent.AddSubCategory(newItem); 
      else 
       result.Add(newItem); 
     } 
     return result; 
    } 
+0

这太棒了!有用!非常感谢先生!只要我能把我所有的东西给你!大!先生,再次感谢! :d – chlkdst

0

你必须编写递归代码来实现这一点。

//First of all, find the root level parent 
int baseParent = "0"; 
// Find the lowest root parent value 
foreach (var selection in collection) 
{ 
    //assign any random parent id, if not assigned before 
     if (string.IsNullOrEmpty(baseParent)) 
     baseParent = selection["PrntID"]; 

    //check whether it is the minimum value 
    if (Convert.ToInt32(selection["PrntID"]) < Convert.ToInt32(baseParent)) 
     baseParent = selection["PrntID"]; 
} 
//If you are sure that your parent root level node would always be zero, then you could //probably skip the above part. 
//Now start building your hierarchy 
foreach (var selection in collection) 
{ 
    CategoryItem item = new CategoryItem(); 
    //start from root 
    if(selection["Id"] == baseParentId) 
    { 
    //add item property 
    item.Id = selection["id]; 
    //go recursive to bring all children 
    //get all children 
    GetAllChildren(item , collection); 
    } 
} 


private void GetAllChildren(CategoryItem parent, List<Rows> Collection) 
{ 
    foreach(var selection in Collection) 
    { 
    //find all children of that parent 
    if(selection["PrntID"] = parent.Id) 
    { 
     CategoryItem child = new CategoryItem(); 
     //set properties 
     child.Id = selection["Id"]; 
     //add the child to the parent 
     parent.AddSubCategory(child); 
     //go recursive and find all child for this node now 
     GetAllChildren(child, Collection); 
    } 
    } 
} 

注意:这不是完全正常工作的代码。但是,这会让你了解你如何周围并建立一个必须被表示为对象的分层数据结构。

0

装入表中的数据表和拳头找到根节点,并创建根对象

DataRow[] rootRow = table.Select("PrntID = 0"); 
CategoryItem root = new CategoryItem() { CategoryID = (int)rootRow[0]["ID"].ToString(), Name = rootRow[0]["Title"].ToString(), ParentID = (int)rootRow[0]["PrntID"].ToString() }; 

然后你需要调用递归的方法来添加子类别,以下方法

GetCategoryItem((int)rootRow[0]["ID"].ToString(), root); 

变化如你所愿。

public void GetCategoryItem(CategoryItem parant) 
{ 
    DataRow[] rootRow = table.Select("PrntID =" + parant.CategoryID); 
    for (int i = 0; i < rootRow.Length; i++) 
    { 
     CategoryItem child = new CategoryItem() { CategoryID = (int)rootRow[i]["ID"].ToString(), Name = rootRow[i]["Title"].ToString(), ParentID = (int)rootRow[i]["PrntID"].ToString() }; 
     GetCategoryItem(child); 
     parant.SubCategory.Add(child); 
    } 
}