简体   繁体   中英

Skip a row in a for each rowset loop in c#

I am looping through a dataset for each data row

 foreach (DataRow DRow in ds.Tables[0].Rows)

I would like to jump the current row when ever a if statement is true. Any clue how to do it?

Thanks in advance !

Use continue :

 foreach (DataRow DRow in ds.Tables[0].Rows)
 {
     if(expression) 
         continue;
 }

continue skips the remaining part of the foreach block for the current element an continues at the next new element in your collection.

Try this:

foreach (DataRow DRow in ds.Tables[0].Rows)
{
    if(true) // escape condition met
         continue;
}

You are looking for the continue keyword...

foreach (DataRow DRow in ds.Tables[0].Rows) {
    if(condition)
        continue;
}

The continue; instruction tells a loop to skip the rest of the code and move to the next iteration.

foreach (DataRow DRow in ds.Tables[0].Rows)
{
    if (--condition here--) continue;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM