edparry
edparry

Reputation: 718

Creating a 3-level tree structure from a flat list of objects

I have a list of objects like this:

1001
Name
Type
Country

To display these results in a browser, I need a structure that is something like this:

Country1
    Type1
        Name1
        Name2
    Type2
        Name3
Country2
    Type3
        Name4
    Type4

I currently have a List<CustomObject>() which is ordered by country, then type, then name.

I have tried several solutions involving way too many foreach loops, with the latest attempt involving new County and Type objects that can both have Children() to try and make up the structure. However, the process of adding to each of these the correct items within the nested loops simply isn't producing a complete list of any use. I can get to the point of having multiple lists (of County and Type objects), but then combining these with the correct relationships isn't proving straight forward.

Any advice would be greatly appreciated!

Upvotes: 0

Views: 222

Answers (1)

user5226582
user5226582

Reputation: 1986

  • You can use a JSON object to store a tree structure.

  • You can have something like this:
    class typeObject : Dictionary<String, List<String>>{}
    class countryObject : Dictionary<String, List<typeObject>>{}
    Your data then could be stored as List<countryObject>

Upvotes: 1

Related Questions