c# - 如何使用检索到的分层结果集创建对象?

标签 c#

我正在使用 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

便于引用的树状 View

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() 方法访问它的子类别/子类别/子类别。这可能吗?

最佳答案

如果您知道在您的 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;
    }

如果我的假设不正确(即一个节点有可能在其父节点之前出现在 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;
        }
        foreach (CategoryItem newItem in alreadyRead.Values)
        {
            CategoryItem aParent;
            if (alreadyRead.TryGetValue(newItem.ParentID, out aParent))
                aParent.AddSubCategory(newItem);
            else
                result.Add(newItem);
        }
        return result;
    }

关于c# - 如何使用检索到的分层结果集创建对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10827237/

相关文章:

c# - 有没有第三方文件上传工具可以上传大于2GB的文件?

c# - EF core 在 ASP.net core 多层架构中添加迁移

c# - Windows 窗体在闪屏后落后

c# - OOP C# 问题 : Making a Fruit a Pear

c# - 如何终止嵌套循环中的外循环?

c# - 我应该在构建服务器上安装 SDK 吗?

c# - 在 WPF 中动态更改 Setter 值

c# - 如何将 102.555 舍入为 102.55?

c# - 十进制类型的声明后缀

c# - 如何安全地检查动态对象是否有字段