如何创建与分层检索的结果集对象? [英] How to create objects with retrieved Hierarchical result set?

查看:122
本文介绍了如何创建与分层检索的结果集对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#语言。我的问题是,我不知道如何来存储我的检索层次结果设置为我的对象

I am using C# language. My problem is that i don't know how to store my retrieved hierarchical result set to my object.

下面的是我的对象:

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服务器

Here's is my sample retrieve data set from MSSQL server

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级深。
我希望将其存储在这样一个像这样的:

My problem is how to do I store this result to my "CategoryItem Object". I don't have any clue do I need to use iteration for this? Specially when the node is 2 level-deep. I want to store it in such a like this:

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



这个我可以在项目对象,每个对象挖,我可以访问它的子类/孩子/我用类的GetSubCategory()方法的孩子。这可能吗?

with this I can dig every objects in the 'items' object and I can access its sub-category / child / children using the GetSubCategory() method of my class. Is this possible?

推荐答案

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

If you know that in your DataSet a node will never appear before its parent, you can use this code. Here you keep track of the already read items in a Dictionary when you can look for parents of the newly read nodes. If you find the parent you add the new item to its children, otherwise it's a first level node.

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

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

If my assumption isn't true (i.e. it is possible for a node to appear in the DataSet before its parent), you have to first read all the nodes (and put them in the Dictionary), then loop through the same Dictionary to build the result. Something like this:

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

这篇关于如何创建与分层检索的结果集对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆