Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将字典转换为二维表od对象?_C#_.net - Fatal编程技术网

C# 如何将字典转换为二维表od对象?

C# 如何将字典转换为二维表od对象?,c#,.net,C#,.net,我有这样一个代码: Dictionary<string, string> dict; . . some[2] = new object[,] { {"key1", "value1"}, {"key2", "Value2"}, {"key3", "Value3"}, }; 字典dict; . . some[2]=新对象[,] { {“key1”,“value1”}, {“key2”,“Value2”}, {“key3”,“Value3”}, }; 如何将dict中的键和值自动插

我有这样一个代码:

Dictionary<string, string> dict;
.
.
some[2] = new object[,]
{
{"key1", "value1"},
{"key2", "Value2"},
{"key3", "Value3"},
}; 
字典dict;
.
.
some[2]=新对象[,]
{
{“key1”,“value1”},
{“key2”,“Value2”},
{“key3”,“Value3”},
}; 

如何将dict中的键和值自动插入到某些[2]中?谢谢

您始终可以循环执行此操作,例如:

Dictionary<string, string> dict = new Dictionary<string, string>()
{
    { "1", "A" },
    { "2", "B" },
    { "3", "C" },
    { "4", "D" }
};

var some = new string[dict.Keys.Count, 2];

int i = 0;
foreach(var item in dict)
{
    some[i, 0] = item.Key;
    some[i, 1] = item.Value;
    i++;
}

您可以通过foreach语句循环整个字典:

foreach (var dictItem in dict)
{
    var key = dictItem.Key;
    var value = dictItem.Value;
    // your code to add them somewhere.
}

这是一个非常琐碎的
dict.Select(x=>new[]{x.Key,x.Value})。ToArray()
如果你问一些非常琐碎的问题,你会有一段不愉快的时光(插入滑雪教练meme)@Will这不是一个锯齿数组吗?@user3185569这是什么?你的意思是在问题中,还是在琐碎的回答中?哪两种都是锯齿阵列?
foreach (var dictItem in dict)
{
    var key = dictItem.Key;
    var value = dictItem.Value;
    // your code to add them somewhere.
}