How to create multidimensional array in C#?

落花浮王杯 提交于 2019-12-25 02:08:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!