简体   繁体   中英

How to store hierachical data in C#

I want to store all makes and models of the cars in my console application.

So I can print them like this:

     - Toyota
         Corolla      
         Camry  
         ...

     - Nissan
        Altima  
        Sentra  
        ...

So, I assume I need some kind of hierachical data. I was thinkng about making Class Make and Class Model. Make Has a list of models. And Then I have list of Makes. But this looks overly complicated. What is the best solution for this kind of things?

It depends what you mean by, "store". In disk? In memory for the app?

Databases are good for persistent storage of this kind of thing. If that seems overkill but you need to store on disk, you could use a CSV or XML file. If you want it built-in to your app, you could create it programatically, but better to embed a resource and read it into a suitable structure (see below)..

Depending on what you're going to do in your app/program, there are many, many, many, many ways of storing and representing this data. An object graph (using classes, inheritance etc), a DataSet, a tree,...

The simplest in-memory storage that comes to mind is a Dictionary<string, List<string>> , which you populate with a mapping from makes to lists of models. Mind you, this isn't so handy if you want to know what make(s) a given model is associated with, so it's important to understand the common usage patterns before settling on a structure.

If you want to store this in a database, then you'd have to think about a schema, probably something like this:

Make  ( MakeName,
        PRIMARY KEY MakeName )

Model ( MakeName,
        ModelName,
        FOREIGN KEY MakeName REFERENCES Make(MakeName),
        PRIMARY KEY (MakeName, ModelName) )

真正取决于品牌和型号对您的应用程序意味着什么,如果它们只是字符串,那么您可以使用诸如dictionary<string,list<string>>之类的dictionary<string,list<string>>的字典,该字典的键将为make,而值则为型号清单

class CarModel {
    public CarModel(string name){
        CarModelName = name;
        CarSubModels = new IList<CarModel>();
    }

    public string CarModelName {get;set;}
    public IList<CarModel> CarSubModels {get;private set;}
    public void AddSubModel(CarModel car){
        if (car == null)
            throw new ArgumentNullException("car");
        if (!CarSubModels.Contains(car))
            CarSubModels.Add(car);
    }
    public void RemoveSubModel(CarModel car){
        if (car == null)
            throw new ArgumentNullException("car");
        if (CarSubModels.Contains(car))
            CarSubModels.Remove(car);
    }

    public void Print(int level=0){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < level; i++)
            sb.Append(' ');
        sb.Append(CarModelName);
        string line = sb.ToString();
        Console.Writeline(line);
        foreach (CarModel car in CarSubModels)
            car.Print(level+1);            
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM