Search code examples
c#listenumeration

Add Items to two list with some sort of reference/hierarchy


Please bear with me with this vague explanation, I'm not quite sure if it's understandable..

Let's say I have two items that need to be added to a collections/a list. I need to have some sort of reference to an id. So, say my list consists of id-entries like 1, 2, 5, 11. Now each id has several items (another array, if you will) referred to. So id 1 has three items (strings), id 2 has ten, id 5 has two etc. You could think of a data table, where my first col has ids and the second one the items.

ID    Item
1     test
1     test2
2     test3
2     test4
5     test5

I used List<List<string>> and added two lists where I filled the first one with the ids and the second one with the items, but then there is no reference.

I want to have something like:

List ID:
 1
        List Items:
        test
        test2
 2      test3

I thought of creating a data table and ad the specific rows and cols but I'm not sure if this is the best way to do it. I want to keep this in some sort of collection, since I eventually need to iterate through this collection in a loop.

Any help is appreciated. And sorry for this explanation.


Solution

  • JohnWu's suggestion solved my problem. Since I also needed a reference to my items, I used a nested list in conjunction with the Dictionary.

    Dictionary<int, List<List<string>>>

    With this I'm able to access both lists which refer to certain IDs.