C# 使用字符串[]中字符串[]的每个排列创建字典复合键

C# 使用字符串[]中字符串[]的每个排列创建字典复合键,c#,loops,dictionary,types,composite-key,C#,Loops,Dictionary,Types,Composite Key,我有一个字典,它充当另一个字典的复合键 我还有一些string[]数组,其中包含用作复合键的必要字符串 例如: //Dictionary that will use composite key Dictionary<Dictionary<string, string>, decimal> lookUpDictionary = new Dictionary<Dictionary<string, string>, decimal>(); //The

我有一个
字典
,它充当另一个
字典
的复合键

我还有一些
string[]
数组,其中包含用作复合键的必要字符串

例如:

//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};
对于第二个阵列:

xx xx
xx yy
xx zz
yy xx
yy yy
yy zz
zz xx
zz yy
zz zz
我尝试过的是这种嵌套循环算法,它似乎能够以正确的顺序获得我想要的排列

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
            {

            }


        }

    }
}
Dictionary compositeKey=new Dictionary();
字符串[]数组1={“aa”,“bb”,“cc”};
字符串[]数组2={“xx”、“yy”、“zz”};
字符串[][]arrayOfArrays={array1,array2};
//对于arrayofarrays[][]中的每个字符串[]
foreach(arrayOfArrays中的字符串[]数组)
{
//对于arrayOfArrays[]
foreach(数组中的字符串字)
{
//获取每个组合,包括它本身
foreach(数组中的字符串字SPAIR)
{
字符串[]排列={word,wordsPair};
Console.WriteLine(置换[0]+置换[1]);
尝试
{
//Add(置换[0],置换[1]);
//字符串值=复合键[排列[0]];
//控制台写入线(值);
}
抓住
{
}
}
}
}
try-catch块中的部分是我遇到问题的地方。我似乎无法正确创建
组合键。这是因为嵌套循环,还是我使用的字典完全错误

编辑:看来我用错了字典。对于这里应该使用的数据类型有什么建议吗


任何建议都将不胜感激。

在创建所有可能的组合时,您正在寻找笛卡尔连接,在您的情况下,它本身就是一个数组。它可以在Linq的帮助下轻松实现

测试:

结果:

[aa, aa]
[aa, bb]
[aa, cc]
[bb, aa]
[bb, bb]
[bb, cc]
[cc, aa]
[cc, bb]
[cc, cc]
[(aa, aa), value for aa & aa]
[(aa, bb), value for aa & bb]
[(aa, cc), value for aa & cc]
[(bb, aa), value for bb & aa]
[(bb, bb), value for bb & bb]
[(bb, cc), value for bb & cc]
[(cc, aa), value for cc & aa]
[(cc, bb), value for cc & bb]
[(cc, cc), value for cc & cc]
请注意,您不应该将
string[]
用作字典中的键,因为数组的实现不会覆盖
GetHashCode
Equals

   // Don't do this! Do not use string[] as a key...
   Dictionary<string[], string> demo = new Dictionary<string[], string>() {
     {new string[] {"a", "b"}, "c"},
   };

   string[] key = new string[] {"a", "b"};     

   // ... And that's the reason why:
   if (!demo.ContainsKey(key))
     Console.WriteLine("Oops!");

字典必须有唯一的键。您多次使用同一密钥。所以你的字典一定是Dictionary@jdweng我真的不想在我的复合键中有一个列表(或者这里有必要吗?)。我想以复合键(字典)的形式将两个字符串放入字典中,并从中检索一个小数。@jdweng我现在明白了,我正试图以一种我不应该使用的方式使用字典。您是否建议使用不同的数据类型,或者如何使用Dictionary方法的示例?如果您需要复合密钥,请使用KeyValuePair作为密钥。
[aa, aa]
[aa, bb]
[aa, cc]
[bb, aa]
[bb, bb]
[bb, cc]
[cc, aa]
[cc, bb]
[cc, cc]
   // Don't do this! Do not use string[] as a key...
   Dictionary<string[], string> demo = new Dictionary<string[], string>() {
     {new string[] {"a", "b"}, "c"},
   };

   string[] key = new string[] {"a", "b"};     

   // ... And that's the reason why:
   if (!demo.ContainsKey(key))
     Console.WriteLine("Oops!");
  string[] array1 = { "aa", "bb", "cc" };

  Dictionary<Tuple<string, string>, string> dictionary = array1
    .SelectMany(left => array1, (left, right) => new Tuple<string, string>(left, right))
    .ToDictionary(item => item, 
                  item => "value for " + string.Join(" & ", item.Item1, item.Item2));

  Console.WriteLine(string.Join(Environment.NewLine, dictionary));
[(aa, aa), value for aa & aa]
[(aa, bb), value for aa & bb]
[(aa, cc), value for aa & cc]
[(bb, aa), value for bb & aa]
[(bb, bb), value for bb & bb]
[(bb, cc), value for bb & cc]
[(cc, aa), value for cc & aa]
[(cc, bb), value for cc & bb]
[(cc, cc), value for cc & cc]