首页 > 解决方案 > 在c#中遍历2行相同的列表

问题描述

  1. 我正在尝试使用“for循环”遍历 (List<List>),然后在添加到文本框中下一行的相应列时找到所选列的总和。即第 1 行的列添加到第 2 行的列,然后创建一个具有总和值的新行。文本框中的行值:
    03 06 16 18 58
    20 43 51 55 15
    08 24 43 28 39
  1. 在上面添加行列值之后,我希望有两行的列值如下: 需要的值:
    23 49 67 73 73
    28 67 43 83 54
  1. 我尝试了以下方式,但它并没有总结所有三行。我真的不知道我哪里弄错了!
  for (int row1 = 0; row1 < textinsideTextbox.Count - 1; row1++)<br/>
  {
     for (int nextRow = row1 + 1; nextRow < textinsideTextbox.Count - 1; nextRow ++)
     { 
       for (int columnselector = 0; columnselector < availableColumns.Count; columnselector++) //number of columns
       { 
         int valuesRequired =  textinsideTextbox[row1][columnselector] + textinsideTextbox[nextRow][columnselector];              
       } 
     }
   }

标签: c#

解决方案


您可以使用Zip两次:一次组合列表对,一次组合数字对:

var ans = src.Zip(src.Skip(1),(l1,l2) => l1.Zip(l2, (n1,n2) => n1+n2).ToList()).ToList();

推荐阅读