2013-12-10 77 views
0

所以我在這裏有3個foreach。 我跳出其中一個去到另一個,但是,當一個reloop發生第二個和第三個foreach總是獲得相同的值,一次又一次,列表的第一個值。 任何解決方案?我想,它需要在列表的第二循環的第二值,名單上的第三環這是第3值,等.... 截圖:https://www.dropbox.com/s/4hafd53q9u4e2tz/Naamloos.png嵌套foreach每次獲得相同的值

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++; 
+0

「舞臺」和「樂隊」在哪裏更新? –

+0

你可以拿出所有多餘的代碼,做一個簡單的例子,你在每個'list','stage'和'bands'中只有三個元素(比如1,2,3),並顯示你想要的輸出到是?我對你的描述感到困惑。這聽起來像你**不要** 111 112 113 121 122 123 131 132 133 211 212 213 ... ??但這就是傳統的嵌套循環所能做到的。也許你需要一個循環,然後訪問每個的第n個元素? – Floris

+0

'foreach'總是從容器的開始處開始。如果你不想從頭開始,建立一個明確的迭代器對象,它將保留跨越邊界的位置。 – dasblinkenlight

回答

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++; 
} 
+0

你好,這個工程,但返回我錯誤的代碼。屏幕截圖:https://www.dropbox.com/s/k470ooo9kg8xa7e/wrong.png我認爲舞臺也是錯的,但幸運的是這次是正確的 – user2827958

+0

在調試器中運行它,看看你是否能找到問題 - 「Zip」並排排列集合,並按順序從每個集合中返回一個項目。不知道更多關於您的來源或期望的信息,我無法給出明確的答案。 –

+0

發現它,我的壞。非常感謝,我學到了很多! – user2827958

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 
} 

檢查和假設名單,分期行加上適當的邊界更多的東西,和樂隊的可轉位(例如,他們是數組,列表,或類似的東西)。

+0

感謝您的迴應。當我嘗試我得到這個。 https://www.dropbox.com/s/k470ooo9kg8xa7e/wrong.png – user2827958

+0

如果您在評論中指出這一點和D Stanley的代碼都得到了相同的結果,那麼您的創建列表顯然有些不同,舞臺和樂隊。如果您可以發佈該代碼,也許我們可以進一步瞭解。 – neilh