cihadakt
cihadakt

Reputation: 3214

skip iteration outer loop in an inner loop

I have two loops and when I iterating in my inner loop, I want to skip iteration for the outer loop according to my condition; continue; is only skips inner loop. Any idea?

foreach (DataRow row in dt.Rows)
 {
                for (int i = 0; i < 6; i++)
                {
                    if (row[i].Equals(DBNull.Value))
                        //skip iteration to outer loop. Go to next row.
                }
 }

Upvotes: 0

Views: 1226

Answers (2)

Enigmativity
Enigmativity

Reputation: 117185

You could do this:

foreach (DataRow row in dt.Rows)
{
    foreach (int i in Enumerable.Range(0, 6).TakeWhile(n => !row[n].Equals(DBNull.Value)))
    {
        // no need for `if` at all now.
    }
}

Upvotes: 0

Bayeni
Bayeni

Reputation: 1046

I think break will serve the purpos, if you want to get out of the inner loop and continue with the next row of the outer loop

  foreach (DataRow row in dt.Rows)
  {
            for (int i = 0; i < 6; i++)
            {
                if (row[i].Equals(DBNull.Value))
                    break;
            }
     }

Upvotes: 2

Related Questions