本文介绍了如何将2D清单二维数组在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的C#列表:

 列表<应变及GT; listAllData =新的List<应变及GT;();listAllData.Add(新响应(){
    strId =亲presponse.strId,
    则strName =亲presponse.strName
});

然后我将其转换为数组作为这样的:

 对象[]数组2 = listAllData.ToArray();

但是,当我试图用写数据到一个范围:

  rngValues​​.Value =数组2;

我得到一个错误

I am (reasonably) certain this is b/c the resulting array2 is not actually a 2D multidimensional array, but instead an array-of-arrays.

So my question is... How do I get my listAllData into a 2D array?

It is not a "jagged array" meaning there are always 2 elements in each entry.

解决方案

No, it's a 1-D array of Response objects. If you want it in a 2-D array of objects (where the row is the two string properties from the source object) you'll have to build a loop (Linq does not support 2-D arrays):

object[,] array2 = new object[listAllData.Count,2];
for(int i = 0; i < listAllData.Count; i++)
{
     array2[i,0] = listAllData[i].strId;
     array2[i,1] = listAllData[i].strName;
}

这篇关于如何将2D清单二维数组在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 02:22