2013-07-25 1 views
0

내 listbox1 안에 몇 가지 항목이 있는데 각 항목을 사용하여 쿼리를 실행하려면 일단 쿼리가 실행되면 목록 상자에서 해당 항목을 삭제하고 다음 항목을 사용하여 쿼리를 실행하고 싶습니다. 예를 들어, item1이 item1을 삭제하는 것보다 사용되면 목록 상자 안의 다음 항목을 사용하여 쿼리를 실행하십시오. 목록 상자 안에 항목이 없을 때까지 모든 항목에 대해 이렇게하십시오.쿼리를 실행 한 후 각 listbox1 항목을 제거 하시겠습니까?

foreach (string myItems in listBox1.Items) 
{ 
    using (OracleCommand crtCommand = new OracleCommand(select REGEXP_REPLACE(dbms_metadata.get_ddl('" + myItems + "'), conn1)) 
    { 
     string expectedresult = "y"; 
     string dataresult = crtCommand.ExecuteScalar().ToString(); 
     if (expectedresult == dataresult) 
     { 
      //do something and remove the item that has been used to run the query.         
     } 
     else 
     { 

     } 
    } 
} 

답변

1

foreach 루프에서는이 작업을 직접 수행 할 수 없습니다. 그것은 당신에게 예외를 줄 것입니다 '컬렉션이 수정되었습니다; foreach 루프 내에서이 작업을 수행하려고하면 열거 연산에서 '이 실행되지 않을 수 있습니다. 오히려 전체 쿼리를 실행 한 후에 모두 삭제할 수 있습니다.

listBox1.Items.Clear(); 

는 Incase의 당신은 당신이

HashSet<int> ids = new HashSet<int>(); 
ids.Add(yourIdToAdd); 

을 만들고 그에서 실행 ID를 추가 할 수 있습니다 실행 된 항목의 트랙을 유지하려는.

+0

어떻게 HashSet 을 사용할 수 있습니까? – user2619275

+0

@ user2619275 업데이트 된 답변보기 – Ehsan

+0

@EhsanUllah, 왜'Items.Clear()'와'HashSet'을 함께 사용합니까? 하나는 모든 항목을 강제로 제거하고, 다른 하나는 List 대신 HashSet을 사용하는 Brian의 대답입니다. – gunr2171

0

foreach에서는 목록을 반복 할 때 목록을 수정하기 때문에 인라인 제거를 수행 할 수 없습니다. 제거하고 싶은 항목의 색인을 추적하여 별도의 호출로 처리합니다. 같은 뭔가 : 타인의 의견에

List<int> removeIndexes = new List<int>(); 
int i = 0; 
foreach (string myItems in listBox1.Items) 
       { 
        using (OracleCommand crtCommand = new OracleCommand(select REGEXP_REPLACE(dbms_metadata.get_ddl('" + myItems + "'), conn1)) 
        { 
         string expectedresult = "y"; 
         string dataresult = crtCommand.ExecuteScalar().ToString(); 
         if (expectedresult == dataresult) 
         { 
          //do something and remove the item that has been used to run the query.      
          removeIndexes.add(i);    
         } 
         else 
         { 

         } 
        } 
        i++; 
       } 

foreach (int index in removeIndexes) 
{ 
    listBox1.Items.RemoveAt(index); 
} 
1

는 달리, 당신은 항목을 삭제하는 foreach 루프를 사용할 수 있습니다. 핵심은 반복을 시도하기 전에 목록의 copy을 만들어야한다는 것입니다.

끝에 .ToList()을 추가하십시오. ItemsCollection 인 경우 .OfType<string>().ToList()으로 적절한 유형으로 타이프 캐스팅해야합니다.

foreach (string myItems in listBox1.Items.OfType<string>().ToList()) 
{ 
    .... 
} 

지금 당신은 당신을 통해 반복하는 목록을 변경하지 않고 listBox.Items에서 항목을 삭제 무료입니다.

0

첫째, foreach 루프의 목록에서 항목을 제거 할 수 없으며 모음을 변경할 때 예외가 발생합니다. 일반 for 루프를 사용해야합니다. 코드는 다음과 같아야합니다.

for (int i = listBox1.Items.Count -1; i>=0; i--) 
     { 
      string myItems = listBox1.Items[i]; 
      using (OracleCommand crtCommand = new OracleCommand(select REGEXP_REPLACE(dbms_metadata.get_ddl('" + myItems + "'), conn1)) 
         { 
          string expectedresult = "y"; 
          string dataresult = crtCommand.ExecuteScalar().ToString(); 
          if (expectedresult == dataresult) 
          { 
           listBox1.RemoveAt(i); 
          } 
          else 
          { 

          } 
         } 
        } 
+0

당신은'listBox1.Items [i]'하고 있지만'RemoveAt (0)'입니다. 'RemoveAt (i)'를 의미하지 않습니까? – gunr2171

+0

고마워. 편집 됨. – CristisS

관련 문제