嵌套的foreach每次都获得相同的值

时间:2013-12-10 16:28:14

标签: c# foreach

所以我在这里有3个foreach's。 我跳出一个去到另一个但是,当一个reloop发生时,第二个和第三个foreach总是一遍又一遍地得到相同的值,列表的第一个值。 有解决方案吗我希望它在第二个循环中获取列表的第二个值,在第三个循环上获取列表的第3个值等等.... 屏幕截图:https://www.dropbox.com/s/4hafd53q9u4e2tz/Naamloos.png

foreach (LineUp ssc in list)
{
    Row r = new Row() { RowIndex = rownumber };
    Cell c1 = new Cell() { CellReference = "A" + rownumber, DataType = CellValues.String, CellValue = new CellValue(ssc.Date) };
    Cell c2 = new Cell() { CellReference = "B" + rownumber, DataType = CellValues.String, CellValue = new CellValue(ssc.From) };
    Cell c3 = new Cell() { CellReference = "C" + rownumber, DataType = CellValues.String, CellValue = new CellValue(ssc.Until) };
    r.Append(c1, c2, c3);
    data.Append(r);

    foreach (Stage ssc2 in stages)
    {                      
            Row r2 = new Row() { RowIndex = rownumber };
            Cell c4 = new Cell() { CellReference = "D" + rownumber, DataType = CellValues.Number, CellValue = new CellValue(ssc2.Name) };
            r2.Append(c4);
            data.Append(r2);
    }

    foreach (Band ssc3 in bands)
    {
            Row r3 = new Row() { RowIndex = rownumber };
            Cell c5 = new Cell() { CellReference = "E" + rownumber, DataType = CellValues.Number, CellValue = new CellValue(ssc3.Name) };
            r3.Append(c5);
            data.Append(r3);
            break;
    }
    break;
}                   
rownumber++;

2 个答案:

答案 0 :(得分:2)

因为您每次都在迭代stagesbands,但是在第一个值之后打破。

听起来就像你想要Zip三个集合一样:

var zippedList = list.Zip(stages, (l, s) => new {ssc = l, stage = s})
                     .Zip(bands, (ls, b) => new {ls.ssc, ls.stage,  band = b)};
foreach (var item in zippedList)
{
    Row r = new Row() { RowIndex = rownumber };
    Cell c1 = new Cell() { CellReference = "A" + rownumber, DataType = CellValues.String, CellValue = new CellValue(item.ssc.Date) };
    Cell c2 = new Cell() { CellReference = "B" + rownumber, DataType = CellValues.String, CellValue = new CellValue(item.ssc.From) };
    Cell c3 = new Cell() { CellReference = "C" + rownumber, DataType = CellValues.String, CellValue = new CellValue(item.ssc.Until) };
    r.Append(c1, c2, c3);
    data.Append(r);

    Row r2 = new Row() { RowIndex = rownumber };
    Cell c4 = new Cell() { CellReference = "D" + rownumber, DataType = CellValues.Number, CellValue = new CellValue(item..stage.Name) };
    r2.Append(c4);

    data.Append(r2);

    Row r3 = new Row() { RowIndex = rownumber };
    Cell c5 = new Cell() { CellReference = "E" + rownumber, DataType = CellValues.Number, CellValue = new CellValue(item.band.Name) };
    r3.Append(c5);

    data.Append(r3);

    rownumber++;
}

答案 1 :(得分:1)

foreach是(大约)

周围的语法糖
var enumerator = stages.GetEnumerator();
while (enumerator.MoveNext()) {
    var ssc2 = enumerator.Current;
    // your loop code
}

所以你的嵌套foreach循环得到一个新的枚举器,并且每次都以第一个值重新开始。你可能想要更多的东西

for (var rowNumber = 0; rowNumber < list.Length; rowNumber++)
{
    var ssc = list[rowNumber];
    // work with ssc

    var ssc2 = stages[rowNumber];
    // work with ssc2

    var ssc3 = bands[rowNumber];
    // work with ssc3
}

加上适当的边界检查并假设列表,阶段和波段是可索引的(例如,它们是数组,列表或类似的东西)。