如何在C#中创建多维数组? [英] How to create multidimensional array in C#?

查看:52
本文介绍了如何在C#中创建多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本字典,我喜欢通过 C# 从中创建多维数组.

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];                                         
}

我需要在 foreach 外观中添加代码,以便它可以创建如下所示的内容:

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 } };

请注意,Item1,Item2 将来自 str1,而 1,2 将来自 int1.

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.

推荐答案

您可以在这样的一个查询中完成.Linq 在转换数据方面非常出色.如果之后你想把它变成 JSON,你可以使用一个库,比如 Json.NET.

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();

这篇关于如何在C#中创建多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆