用数据填充分层类结构

发布于 2024-10-09 22:14:52 字数 1396 浏览 0 评论 0原文

我有一个像这样的分层类结构:

类别 ->模板->实例

一个类别包含多个模板,一个模板包含多个实例。

如果我通过所有 3 个表的联接查询数据库中的数据,数据看起来像这样:(

CategoryID | CategoryName | CategoryType | TemplateID | TemplateName | TemplateXYZ | InstanceID | InstanceName  
1 | "CatA" | "TypeX" | 1 | "TempA" | "X" | 1 | "InstA" 
1 | "CatA" | "TypeX" | 1 | "TempA" | "X" | 2 | "InstB"
1 | "CatA" | "TypeX" | 1 | "TempA" | "X" | 3 | "InstC"
1 | "CatA" | "TypeX" | 1 | "TempB" | "Y" | 4 | "InstD"

只是一个例子,真实的数据表有更多的列)

C# 中填充数据的最佳/常见方法是什么当使用数据读取器循环浏览此类数据时,是否会使用此类数据?

从我的想法来看,我会这样做:

while(data.Read())
{
  // Create new objects each time the ID changes and read the data from the first row
  if(data["CategoryID"] != lastCategoryID) {
    lastCategoryID = data["CategoryID"];
    cat = new Category(data["CategoryName"], data["CategoryType"]);
    catList.Add(cat);
  }
  if(data["TemplateID"] != lastTemplateID) {
    lastTempateID = data["TemplateID"];
    template = new Template(data["TemplateName"], data["TemplateXYZ"]));
    cat.Templates.Add(template);
  }
  template.Instances.Add(new Instance(data["InstanceID"], data["InstanceName"]);
}

是否有更好、更优雅的解决方案来填充分层类对象?也许使用 LINQ 或字典?

注意:这个问题与我关于 的其他问题相关从数据库收集分层数据的最佳方式。我把它分开,因为这是两个不同的问题。

I have a hierarchical class structure like this:

Category -> Template -> Instance

A category contains multiple templates, a template contains multiple instances.

If I query the data from the database via a join over all 3 tables, the data looks something like this:

CategoryID | CategoryName | CategoryType | TemplateID | TemplateName | TemplateXYZ | InstanceID | InstanceName  
1 | "CatA" | "TypeX" | 1 | "TempA" | "X" | 1 | "InstA" 
1 | "CatA" | "TypeX" | 1 | "TempA" | "X" | 2 | "InstB"
1 | "CatA" | "TypeX" | 1 | "TempA" | "X" | 3 | "InstC"
1 | "CatA" | "TypeX" | 1 | "TempB" | "Y" | 4 | "InstD"

(just an example, the real data tables have a lot more columns)

What is the best/common way in C# to fill the classes with this kind of data when cycling through it with a data reader?

From the top of my head I would do it this way:

while(data.Read())
{
  // Create new objects each time the ID changes and read the data from the first row
  if(data["CategoryID"] != lastCategoryID) {
    lastCategoryID = data["CategoryID"];
    cat = new Category(data["CategoryName"], data["CategoryType"]);
    catList.Add(cat);
  }
  if(data["TemplateID"] != lastTemplateID) {
    lastTempateID = data["TemplateID"];
    template = new Template(data["TemplateName"], data["TemplateXYZ"]));
    cat.Templates.Add(template);
  }
  template.Instances.Add(new Instance(data["InstanceID"], data["InstanceName"]);
}

Is there a better, more elegant solution to fill the hierarchical class objects? Maybe using LINQ or Dictionaries?

Note: This question is related to my other question about the best way to gather hierarchical data from a DB. I split it up because this are two separate issues.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

节枝 2024-10-16 22:14:52

你所做的似乎是一个很好的工作方式。只需确保按照您拥有的 ID 列对查询中的数据进行排序即可。按类别排序,然后按模板排序。这将确保您不会返回到这些 ID 之一并再次创建该对象。

另外 - 如果一个模板可以属于多个类别,则您必须将每个模板存储在列表中的某个位置,以确保不会在类别中重复它们。

What you do seems like a good way to work it. Just make sure you sort the data in your query by and ID columns you have. Sort by category then template. This will ensure you don't go back to one of those IDs and create the object again.

Also - if a template can be in multiple categories, you will have to store each template in a list somewhere to make sure you don't duplicate them over categories.

不交电费瞎发啥光 2024-10-16 22:14:52

当您从数据读取器读取数据时,用每行的数据填充一个对象。此时,不必担心重复项:

var rawData = new List<Incoming>();
while (data.Read())
{
    rawData.Add( new Incoming(data[0], data[1],data[2],data[3],data[4],data[5],data[6],data[7]));
}

public class Incoming
{
    public int CategoryID { get; set; }
    public string CategoryName { get; set; }
    public int CategoryType { get; set; }
    public int TemplateID { get; set; }
    public string TemplateName { get; set; }
    public int TemplateXYZ { get; set; }
    public int InstanceID { get; set; }
    public string InstanceName { get; set; }

    public Incoming(int categoryID , string categoryName , int categoryType , int templateId,string templateName ,int templateXYZ , int instanceID , string instanceName    )
    {
        CategoryID =categoryID;
        CategoryName = categoryName; CategoryType = categoryType; TemplateID = templateId;
        TemplateName = templateName; TemplateXYZ = templateXYZ; InstanceID = instanceID; InstanceName = instanceName; 
    }
}

您可以使用 LINQ 来获取层次结构的各个级别:

varcategories = rawData.GroupBy (d => d.CategoryID );

As you read from the data reader, populate an object with the data from each row. At this point don't worry about the duplicates:

var rawData = new List<Incoming>();
while (data.Read())
{
    rawData.Add( new Incoming(data[0], data[1],data[2],data[3],data[4],data[5],data[6],data[7]));
}

where

public class Incoming
{
    public int CategoryID { get; set; }
    public string CategoryName { get; set; }
    public int CategoryType { get; set; }
    public int TemplateID { get; set; }
    public string TemplateName { get; set; }
    public int TemplateXYZ { get; set; }
    public int InstanceID { get; set; }
    public string InstanceName { get; set; }

    public Incoming(int categoryID , string categoryName , int categoryType , int templateId,string templateName ,int templateXYZ , int instanceID , string instanceName    )
    {
        CategoryID =categoryID;
        CategoryName = categoryName; CategoryType = categoryType; TemplateID = templateId;
        TemplateName = templateName; TemplateXYZ = templateXYZ; InstanceID = instanceID; InstanceName = instanceName; 
    }
}

then you can use LINQ to get the individual levels of the hierarchy out:

var categories = rawData.GroupBy (d => d.CategoryID );

季末如歌 2024-10-16 22:14:52

类似下面的内容将为您提供直接上课的方法:

string[] allLines = File.ReadAllLines("datafile.dat");

var query = from line in allLines
            let data = line.Split('|')
            select Category
                {
                    CategoryID = data[0],
                    CategoryName = data[1],
                    CategoryType = data[2], 
                    Template = new Template { TemplateID = data[3],
                                              TemplateXYZ = data[4],
                                              Instance = new Instance { InstanceID = data[5],
                    InstanceName = data[6] }
                                    }
                };

Something like the following would provide you with a direct to class approach:

string[] allLines = File.ReadAllLines("datafile.dat");

var query = from line in allLines
            let data = line.Split('|')
            select Category
                {
                    CategoryID = data[0],
                    CategoryName = data[1],
                    CategoryType = data[2], 
                    Template = new Template { TemplateID = data[3],
                                              TemplateXYZ = data[4],
                                              Instance = new Instance { InstanceID = data[5],
                    InstanceName = data[6] }
                                    }
                };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文