简体   繁体   English

C# 试图用对象填充二维对象数组

[英]C# Trying to fill a 2-dimensional Object-Array with Objects

I am creating (trying to at least) a MySQL application in C#我正在 C# 中创建(至少尝试)一个 MySQL 应用程序

        int i = 0;
        Object[,] oResult = new Object[i,Reader.FieldCount];
        while (Reader.Read())
        {
            Reader.GetValues(oResult[i,]);
            i++;
        } 

this part should fill the 2-dim Object array with the returned Object[] of the GetValues() method however, i cant get the synrtax right.这部分应该用 GetValues() 方法返回的 Object[] 填充 2-dim Object 数组,但是,我无法获得正确的语法。 i is the number of rows, so for each row my reader reads, the array gets larger (at least i hope so) but i cant specify how to fill the second iteration. i 是行数,因此对于我的读者读取的每一行,数组都会变大(至少我希望如此),但我无法指定如何填充第二次迭代。 in theory there shouldnt be one, since i want the method to fill this whole row.理论上不应该有一个,因为我希望该方法可以填充整行。 in a 1-dim array this works just fine.在一个 1-dim 数组中,这工作得很好。

right now, i would say the best might be to switch to an list<object[]> instead of the 2-dim array and call it a day.现在,我想说最好的办法可能是切换到 list<object[]> 而不是 2-dim 数组,然后就这样结束了。 even tho i think maybe maaaaaaaaaaaaaaaaaaaaaaybe i could be required to have the 2-dim array即使我认为也许 maaaaaaaaaaaaaaaaaaaaaaybe我可能需要拥有 2-dim 阵列

EDIT: GetValues does NOT RETURN object[], rather fills an given Object[] with values编辑:GetValues 不返回对象 [],而是用值填充给定的对象 []

TL;DR how to fill a 2dim array with an array (ar[1,"this hole part should be filled*]) TL;DR 如何用数组填充 2dim 数组(ar[1,"这个孔部分应该填充*])

It's quite a problem to resize an array, and you don't know apriori Length .调整数组大小是一个相当大的问题,而且您不知道先验Length That's why let's store the records read in a List<T> :这就是为什么让我们将读取的记录存储在List<T>中:

    List<object[]> data = new List<object[]>();

    while (Reader.Read()) {
      object[] record = new object[Reader.FieldCount];  

      Reader.GetValues(record);
       
      data.Add(record); 
    }       

Then turn the list into 2d array:然后将列表转换为二维数组:

    Object[,] oResult = new object[data.Count, Reader.FieldCount];

    for (int r = 0; r < data.Count; ++r)
      for (int c = 0; c < data[r].Length; ++c)
        oResult[r, c] = data[r][c]; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM