C#嵌套列表存储不正确的值

时间:2016-05-26 05:47:49

标签: c# list

我在c#中遇到嵌套列表。我正在使用Excel数据填充列表。为此,我存储来自subdlist2中列的所有数据,然后将其添加到我的主列表termsList。
但是,在我显示列表内容的最后,值不是来自多行的值,而是由最后一列的值替换。列表内容存储为参考而不是值吗?
示例:我有5行,具有不同的值,第五个值替换全部。

for (i = 0; i < num_speed; i++)
   {
       lastrow_current = GetLastUsedRow(currentWorksheet, 12 + i * 11);
       for (int j = 0; j < lastrow_current; j++)
       {
           if (currentWorksheet.Cells[j + 6, 12 + i * 11].Value != null)
           {
               if (j == 0)
               {
                   sublist2.Add(currentWorksheet.Cells[j + 6, 13 + i * 11].Value.ToString());
               }
               else
               {
               sublist2.Add(currentWorksheet.Cells[j + 6, 13 + i * 11].Value.ToString());
               }
           }
       }
       termsList.Add(sublist2);
   }
   termsList.Add(sublist2);
   foreach (var sublist3 in termsList)
   {
       foreach (var value in sublist3)
       {
           Console.Write(value);
           Console.Write(' ');
       }
       Console.WriteLine();
   }

2 个答案:

答案 0 :(得分:0)

很难从你的简短例子中说出来。请更新示例,以便包含列表的初始化

但列表确实是一个参考对象。您应该为每一行实例化一个新列表。

您的代码应如下所示:

var rows = new List<List<string>>();
for (row = 0; row < maxRow; row++)
{
  var row = new List<string>();
  for (column = 0; column < maxColumn; column++)
  {
    var value = sheet[row, column];
    row.Add(value);
  }
  rows.Add(row);
}

请注意,您应该为变量指定一个明确的名称(而不是i,j)。此外,您应该使用临时变量将数据放入: 不要将currentWorksheet.Cells [j + 6,13 + i * 11] .Value放在所有地方,但只使用一次

var value = currentWorksheet.Cells[j + 6, 13 + i * 11].Value;

然后重新使用值...

答案 1 :(得分:0)

您的源代码中有许多令人困惑的事情,所以首先我尝试对它进行一些排序:

// List<List<string>> termsList = the list to fill with nested lists
//        List<string> sublist2 = a list created outside the loop
//                    num_speed = number of columns to process

for (i = 0; i < num_speed; i++)
{
    var currentColumn   = 12 + i * 11; // taken from if (currentWorksheet.Cells[j + 6, 12 + i * 11]
    var differentColumn = 13 + i * 11; // taken from sublist2.Add(currentWorksheet.Cells[j + 6, 13 + i * 11]

    var lastRowOfCurrentColumn = GetLastUsedRow(currentWorksheet, currentColumn);

    for (int j = 0; j < lastRowOfCurrentColumn; j++)
    {
        var currentRow = j + 6; // taken from currentWorksheet.Cells[j + 6,

        var currentCell = currentWorksheet.Cells[currentRow, currentColumn];
        var differentCell = currentWorksheet.Cells[currentRow, anotherColumn];

        // here you are checking if the cell has a value ...
        if (currentCell.Value != null)
// was  if (currentWorksheet.Cells[j + 6, 12 + i * 11].Value != null)
        {
            if (j == 0)
            {
                // ... but here ...
                sublist2.Add(differentCell.Value.ToString());
        // was  sublist2.Add(currentWorksheet.Cells[j + 6, 13 + i * 11].Value.ToString());
            }
            else
            {
                // ... and here you are adding a value from a cell
                // out of a different column to the list
                sublist2.Add(differentCell.Value.ToString());
        // was  sublist2.Add(currentWorksheet.Cells[j + 6, 13 + i * 11].Value.ToString());
            }
        }
    }

    // Here you are adding the same list to the parent list
    // again and again for every row ...
    termsList.Add(sublist2);
}

// ... and outside the loop you are adding that list again to the parent
termsList.Add(sublist2);

因此,除了检查一个单元格的值,但使用其他单元格中的值添加到列表中之外,您在每一行都使用相同的列表。

要为每一行创建单独的列表并使用实际的已检查列,您必须更改代码:

for (int j = 0; j < lastRowOfCurrentColumn; j++)
{
    var listForRow = new List<string>();
// ...
                listForRow.Add(currentCell.Value.ToString());
// ...
    termsList.Add(listForRow);
}

并在循环外删除多余的嵌套列表。