0

Is it possible to "seek" through an loop? Based on certain conditions, I want to do a "continue in place," to fast forward through a loop. Like this:

for(var thing in things) { // Do stuff if(something) { // Move iteration forward until the iteration object ("thing") meets the right condition while(true) { // Move the iteration forward...somehow [Missing code goes here] if(thing.Property == somevalue) { break; } } } // Do more stuff on the new value of "thing" } 

I could use continue, but I don't want to go back to the top of the loop. I want to cycle forward through the objects in the enumerator, then just pick up where I left off.

I'm guessing this is not possible. If not, what would be the best logic to emulate what I want to do.

11
  • 3
    Cannot you filter the things collection before iteration? Commented Mar 12, 2015 at 20:13
  • 3
    If you can use a for loop instead, it's easy (and dangerous) to adjust the loop counter at any point in the loop. If you have an IEnumerator, just call MoveNext() until your condition is hit. Commented Mar 12, 2015 at 20:16
  • what is the datatype of the things object.. if it's a datatable or dataset you could just use the .Select method to filter what you need then you will have no need to do all the forward seeking Commented Mar 12, 2015 at 20:17
  • 1
    Looks like a LINQ/Lamdba expression is need to filter your collection - which would make your code a lot cleaner Commented Mar 12, 2015 at 20:20
  • 1
    Gosh I hate to see while(true) in code Commented Mar 12, 2015 at 20:21

4 Answers 4

5

Have you considered using a standard for loop instead of the foreach loop you are using?

E.g.

var thing; for (int i = 0; i < things.Length; i++) { thing = things[i]; //do stuff if(something) { while([thing doesn't meet condition] && i < things.Length - 1 ) { thing = things[++i]; } } } 

If you've seen the incrementer before (i++), ++i might seem strange. All it does is increment i before you use it. So, if you entered the while loop on things[5], thing would be set to things[6]. The while loop will also break if you cannot load any more objects.

Sign up to request clarification or add additional context in comments.

3 Comments

Unlike most answers that start with "I can't comment [so I'll answer]", this one is actually a decent answer! I cleaned it up a bit for you. Welcome to StackOverflow.
Yes, this is a good answer. I ended up doing a linked list, where every thing had a reference to the next thing, so I wasn't iterating at all but was rather jumping around an unordered bucket of things.. But this is essentially the same thing -- a for loop is like a linked list, in the sense that there's an implicit link to the next incremental numeric index.
@TimS. Thanks! I always forget that you can inline code as well.
2

Filter things before the loop

var filtered = things.Where(x => x.Property == somevalue); foreach ( var thing in filtered ) { if (something) // Do more stuff on the new value of "thing" } 

1 Comment

This is going to run the code in a very different order. If these operations can be reordered without it affecting them, then this is fine, but if these operations cannot be reordered in this way, then this isn't a valid transformation.
0

you can manually iterate through the iterator

you can take a look here:

Using IEnumerable without foreach loop

Comments

0

You can "rebuild" the logic of the foreach. Note the using (because the foreach disposes the enumerator)

using (var enu = things.GetEnumerator()) { bool success; while (success = enu.MoveNext()) { // Current value (always "valid"): enu.Current; if (something) { while (success = enu.MoveNext()) { // Current value (always "valid"): enu.Current; if (enu.Current == someValue) { break; } } } // Current value (check success before using it): enu.Current; if (success) { // Do more stuff on the new value of "thing" } } } 

The if (res) is necessary because the inner while could "exhaust" the enumerator.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.