Filling the hierarchical structure of classes with data

I have a hierarchical class structure like this:

Category → Template → Instance

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

If I request data from a database through a join across 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, real data tables contain a lot more columns)

What is the best / general way in C # to populate classes with this data type when cycling through it using a data reader?

At the top of my head, I would do it like this:

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 for populating hierarchical class objects? Perhaps using LINQ or dictionaries?

. . , .

+5
3

, , . , , . , . , .

- , -, , .

+3

, . :

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 :

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

+1

- , :

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

All Articles