I have a Dictionary<string,string>
that is acting as the composite key for another Dictionary
.
I also have some string[]
arrays that contains the necessary strings to be used as the composite key.
For example:
//Dictionary that will use composite key
Dictionary<Dictionary<string, string>, decimal> lookUpDictionary = new Dictionary<Dictionary<string, string>, decimal>();
//The composite key dictionary
Dictionary<string, string> compositeKey = new Dictionary<string, string>();
//Array containing strings
string[] array1 = { "aa", "bb", "cc" };
string[] array2 = { "xx", "yy", "zz" };
//A container for the array of individual units
string[][] arrayOfArrays = {array1,array2};
I would like to make the composite key out of every possible combination pair of strings within each array. For example, I'd like to fill the compositeKey thats a Dictionary<string, string>
with every permutation of the array1 string[]
...Such as:
aa aa
aa bb
aa cc
bb aa
bb bb
bb cc
cc aa
cc bb
cc cc
and for the second array:
xx xx
xx yy
xx zz
yy xx
yy yy
yy zz
zz xx
zz yy
zz zz
What I have tried is this nested loop algorithm that seems to work for getting the permutations I want in the right order.
Dictionary<string, string> compositeKey = new Dictionary<string, string>();
string[] array1 = { "aa", "bb", "cc" };
string[] array2 = { "xx", "yy", "zz" };
string[][] arrayOfArrays = {array1,array2};
//for every string[] in the arrayofarrays[][]
foreach(string[] array in arrayOfArrays)
{
//for every string in each of those string[] arrays in the arrayOfArrays[][]
foreach(string word in array)
{
//get every combination including itself
foreach(string wordsPair in array)
{
string[] permutation = { word, wordsPair };
Console.WriteLine(permutation[0] + permutation[1]);
try
{
//compositeKey.Add(permutation[0], permutation[1]);
//string value = compositeKey[permutation[0]];
//Console.WriteLine(value);
}
catch
{
}
}
}
}
The section within the try catch block is where I am having trouble. I can't seem to create the compositeKey
correctly. Is this because of the nested loop, or am I using dictionarys
completely wrong.
Edit: It would seem that I am trying to use dictionary in a wrong way. Is there any suggestion on the type of datatype that should be used here?
Any advice is appreciated.