Filling a hierarchical class structure with data Filling a hierarchical class structure with data database database

Filling a hierarchical class structure with data


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.


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


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