2013-06-26 309 views
0

我有一個列表,其中包含一些值, 現在我想要獲取數組中的ID與數值的所有數據。現在的問題是,我只有1個結果,而數組中有13個整數,所以它不會循環。C#foreach循環

這是我的代碼:其中messages是具有整數的數組。

List<string> messageList = new List<string>(); 
foreach (string i in messages) 
{ 
    Recordset Persons = SDK.Create("R_PERSON", "", "PK_R_PERSON = "+i ,""); 
    if (Persons != null && Persons.RC > 0) 
    {     
     Persons.MoveFirst(); 

     do 
     { 
      string firstname = Persons.Fields["FIRSTNAME"].Value.ToString(); 
      string lastname = Persons.Fields["LASTNAME"].Value.ToString(); 

      personmessages.Add(firstname); 
      personmessages.Add(lastname); 

      PersoninboundSet.MoveNext(); 

     } 
     while (!PersoninboundSet.EOF); 
    } 
    return personmessages; 
} 


messages.Add("Error, didn't work."); 
return messages;// null; 

有人能告訴我我做錯了什麼嗎?

+0

你確定它是不是SDKRecordset PersoninboundSet = IQSDK.CreateRecordset(「R_PERSON」,「」,「PK_R_PERSON =」 +一世 ,」」 );只返回一條記錄,是否通過它? – Bit

+0

您的列表包含字符串,而不是整數。 – Max

+2

您在第一次循環迭代結束時返回personmessages。這意味着循環不可能繼續第一次迭代。我猜這不是你想要的行爲。 –

回答

6

我認爲你需要將回路移出循環。

List<string> personmessages = new List<string>(); 
foreach (string i in messages) 
{ 
    //// Do stuff. 
} 
return personmessages; 
+0

我已經得到了解決方案,我忘記關閉該代碼上方的循環if語句。但是,無論如何謝謝你,我會接受這個答案。 – Matheno

1

正如丹上面提到的,當你在一個函數調用return,你會離開,即使還沒有完成通過環去的功能。如果你想爲循環的每次迭代都有一個單獨的返回值,我建議將每個結果存儲在一個數組中,然後在循環結束時簡單地返回數組。

List<string> personmessages = new List<string>(); 
List[] messageArray = new List[messages.length()]; 
foreach (string i in messages) 
{ 
    SDKRecordset PersoninboundSet = IQSDK.CreateRecordset("R_PERSON", "", "PK_R_PERSON = "+i ,""); 
if (PersoninboundSet != null && PersoninboundSet.RecordCount > 0) 
{ 

    PersoninboundSet.MoveFirst(); 

    do 
    { 
     string firstname = PersoninboundSet.Fields["FIRSTNAME"].Value.ToString(); 
     string lastname = PersoninboundSet.Fields["LASTNAME"].Value.ToString(); 

     personmessages.Add(firstname); 
     personmessages.Add(lastname); 

     PersoninboundSet.MoveNext(); 

    } 
    while (!PersoninboundSet.EOF); 
} 
messageArray[i] = personmessages; 
} 
return messageArray; 
1

它看起來像你已經得到了循環和檢查混合的順序。你想return personmessages以外的foreach循環。

假設你要找到每個messages,並返回如果任何message無法找到,你要移動的foreach循環中的錯誤條件的錯誤。

List<string> personmessages = new List<string>(); 
foreach (string i in messages) 
{ 
    SDKRecordset PersoninboundSet = IQSDK.CreateRecordset("R_PERSON", "", "PK_R_PERSON = "+i ,""); 
    if (PersoninboundSet != null && PersoninboundSet.RecordCount > 0) 
    { 

     PersoninboundSet.MoveFirst(); 

     do 
     { 
      string firstname = PersoninboundSet.Fields["FIRSTNAME"].Value.ToString(); 
      string lastname = PersoninboundSet.Fields["LASTNAME"].Value.ToString(); 

      personmessages.Add(firstname); 
      personmessages.Add(lastname); 

      PersoninboundSet.MoveNext(); 

     } 
     while (!PersoninboundSet.EOF); 
    } 
    else 
    { 
     // *this* i could not be found 
     messages.Add("Error, didn't work."); 
     return messages;// null; 
    } 
} 
return personmessages; // after we've processed *all* messages 

或者,也許你想回來,如果沒有message可以發現一個錯誤。在這種情況下,讓你的錯誤外循環檢查,但檢查Count確定:

List<string> personmessages = new List<string>(); 
foreach (string i in messages) 
{ 
    SDKRecordset PersoninboundSet = IQSDK.CreateRecordset("R_PERSON", "", "PK_R_PERSON = "+i ,""); 
    if (PersoninboundSet != null && PersoninboundSet.RecordCount > 0) 
    { 

     PersoninboundSet.MoveFirst(); 

     do 
     { 
      string firstname = PersoninboundSet.Fields["FIRSTNAME"].Value.ToString(); 
      string lastname = PersoninboundSet.Fields["LASTNAME"].Value.ToString(); 

      personmessages.Add(firstname); 
      personmessages.Add(lastname); 

      PersoninboundSet.MoveNext(); 

     } 
     while (!PersoninboundSet.EOF); 
    } 
} 
if (personmessages.Count == 0) 
{ 
    // none found 
    messages.Add("Error, didn't work."); 
    return messages;// null; 
}  
else 
{ 
    return personmessages; // after we've processed *all* messages 
}