title: Manually increment an enumerator inside foreach loop
body: <p>I have a nested while loop inside a foreach loop where I would like to advance the enumerator indefinitately while a certain condition is met. To do this I try casting the enumerator to IEnumerator&lt; T > (which it must be if it is in a foreach loop) then calling MoveNext() on the casted object but it gives me an error saying I cannot convert it.</p>
  
  <p><em>Cannot convert type 'System.DateTime' to System.Collections.Generic.IEnumerator via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion.</em></p>
  
  <pre><code>        foreach (DateTime time in times)
          {
              while (condition)
              {
                  // perform action
                  // move to next item
                  (time as IEnumerator&lt;DateTime&gt;).MoveNext(); // will not let me do this
              }
  
              // code to execute after while condition is met
           }
  </code></pre>
  
  <p>What is the best way to manually increment the IEnumerator inside of the foreach loop?</p>
  
  <p>EDIT:
  Edited to show there is code after the while loop that I would like executed once the condition is met which is why I wanted to manually increment inside the while then break out of it as opposed to continue which would put me back at the top. If this isn't possible I believe the best thing is to redesign how I am doing it.</p>