0

I have defined a dictionary that its value is itself a dictionary that the values of that inner dictionary are list of strings like so:

Dictionary<string, Dictionary<string, List<string>>> myDictionary;

The structure is complex for me and don't know now how to add values to it and checking if the keys exits, etc.. So I start with:

    Dictionary<string, Dictionary<string, List<string>>> myDictionary = new Dictionary<string, Dictionary<string, List<string>>>();
    if (!myDictionary.ContainsKey("outerKey"))
    {
        myDictionary.Add("someOuterKey", ???);
    }

and then I get stuck. Can you help with writing this?

Bohn
  • 26,091
  • 61
  • 167
  • 254

5 Answers5

3

Here's an example of how to add a value to the outerkey/innerkey structure:

var outerKey = "Some Outer Key";
var innerKey = "Some Inner Key";
var myValue = "Some Value";

if (!myDictionary.ContainsKey(outerKey))
{
    myDictionary.Add(outerKey, new Dictionary<string, List<string>>());
}

if (!myDictionary[outerKey].ContainsKey(innerKey))
{
    myDictionary[outerKey].Add(innerKey, new List<string>());
}

myDictionary[outerKey][innerKey].Add(myValue);
Ian P
  • 12,840
  • 6
  • 48
  • 70
3

That's quite abstract, might help to make it less abstract and then try and understand it.

Consider that the outer Directory could be "Family", inner directory could be "Member" and List could be "Friends", then it makes sense to do the following:

var families = new Dictionary<string, Dictionary<string, List<string>>>();
var members = new Dictionary<string, List<string>>();
var friends = new List<string> { "Fred", "Arnold", "Rebecca" };
members.Add("John", friends);
families.Add("Smith", members);

I always find context helps to work these things out. Just change the names of the variables to whatever makes sense in your project.

Mark

Mark Rabjohn
  • 1,643
  • 14
  • 30
1

Does this help you -

Dictionary<string, Dictionary<string, List<string>>> myDictionary = new Dictionary<string, Dictionary<string, List<string>>>();
if (!myDictionary.ContainsKey("outerKey"))
{

    myDictionary.Add("outerKey",  new Dictionary<string, List<string>>());
}

Sample Add & Get functions

Once it has been initialized, here is how u can add a value to it

 public void Add(string outerKey, string innerKey, string innerValue)
    {
        //add to out dictionary
        if (!myDictionary.ContainsKey("outerKey"))
        {

            myDictionary.Add("outerKey", new Dictionary<string, List<string>>());
        }

        //add to inner dictionary

      if(!myDictionary[outerKey].ContainsKey(innerKey))
      {
          myDictionary[outerKey].Add(innerKey,new List<string>());
      }

        // add to list of inner dictionary
      myDictionary[outerKey][innerKey].Add(innerValue);

    }

Check if the keys are added, if not the fetcch shall return null

public List<string> FetchValue(string outerKey, string innerKey)
    {
        List<string> result = null;
        if(myDictionary.ContainsKey(outerKey))
            result = myDictionary[outerKey][innerKey];

        return result;
    }
Kapoor
  • 1,388
  • 11
  • 21
1

The answer depends a little on what you are trying to achieve. There may even be a data structure that would be easier to use. But lets say you want to add loweststringa and loweststringb for innerstring1 for outerstring (ooohh that is complex).

Then this would do it:

List<string> lowestList = new List<string>();
lowestList.AddRange(new[]{"loweststringa", "loweststringb"});

Dictionary<string, List<string>> innerDictionary = new Dictionary<string, List<string>>();
innerDictionary.Add("innerstring1", lowestList);

Dictionary<string, Dictionary<string, List<string>>> myDictionary = new Dictionary<string, Dictionary<string, List<string>>>();
myDictionary.Add("outerstring", innerDictionary);

The keys you add to myDictionary are of type string. The values of myDictionary are instances of a Dictionary<string, List<string>>.

The keys of these inner dictionaries are of course also of type string and the values are of type List<string>.

If you want to check if "loweststringb" exists in the list for "innerstring1" in the dictionary for "outerstring" you can do the following:

if (myDictionary.ContainsKey("outerstring"))
  if (myDictionary["outerstring"].ContainsKey("innerstring1"))
    if (myDictionary["outerstring"]["innerstring1"].Contains("loweststringb"))
      // hooray it's there!

But be sure you don't insert null references as Dictionaries or Lists in this construction. And the example above is not the most efficient since it iterates through the outermost dictionary three times.

Hope this helps you.

UPDATE: inserting "loweststringc" in the existing list:

myDictionary["outerstring"]["innerstring1"].Add("loweststringc");
René Vogt
  • 43,056
  • 14
  • 77
  • 99
1

Hope this will solve your problem.

Dictionary<string, Dictionary<string, List<string>>> myDictionary = new Dictionary<string, Dictionary<string, List<string>>>();
if (!myDictionary.ContainsKey("outerKey"))
{
   var innerDictiionary= new Dictionary<string, List<string>>();
   var data= new List<sting>(){
    "hello"
};
innerDictiionary.Add("innerKey", data);
myDictionary.Add("outerKey",  innerDictiionary);
}
Anik Saha
  • 4,313
  • 2
  • 26
  • 41