问题
I have a dictionary from which I like to create a multidimensional array through c#.
foreach (KeyValuePair<string, int> pair in rptdata)
{
string s2 = pair.Key;
int s1 = pair.Value;
// var ccdata1 = new[] { new object[] { "Item1", 1 } };
// object value = cdata1[s1,s1];
}
I need to add code inside the foreach look so that it can create something like the following:
var ccdata = new[] { new object[] { "Item1", 1 }, new object[] { "Item2", 2 } };
Note that Item1,Item2 would come from str1 and 1,2 would come from int1.
I am not sure how to iterate though to populate the multidimensional object array.
回答1:
You could do it in one query like this. Linq is pretty great at transforming data. If you want to turn it into JSON afterwards you can use a library, something like Json.NET.
var ccdata = rptdata
.Select( i => new object[]{ i.Key, i.Value } )
.ToArray();
来源:https://stackoverflow.com/questions/7788784/how-to-create-multidimensional-array-in-c