locked
DataTable foreach loop - continue to next row? RRS feed

  • Question

  • User451945003 posted

    How can I continue to the next row in the foreach loop below. 

            foreach (DataRow row in dtGCats.Rows)
            {
                prevCG_ID = holdCG_ID;
                tempCG_ID = Convert.ToInt16(row["CG_ID"]);
                //  Skipping past Excess products (over 4) in Category
                if ( (tempCG_ID == prevCG_ID) && counter1 >= 4);
                      {  
                        counter1++;
                        ExtraItem = 1;
                   //     continue;  ****** WANT TO GO TO NEXT ROW, not leave the loop
                      }
               
                //  Load first row of new GLinks DataTable
                if (Convert.ToInt16(row["rnum"]) == 0)
                {
                    dr = GLinks.NewRow();
                    dr["rnum"] = row["rnum"];
                     . . . . . . . . . .

    Friday, March 11, 2011 2:47 PM

Answers

  • User-292625832 posted

    so you have written correct "continue;" this will start executing next iteration.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 11, 2011 3:08 PM
  • User-1412735316 posted

    I may miss understood your problem.

    If in a loop you want to do some sort of calculation with next values then again want to proceed the current loop then my previous solution is ok.

    If you just want to go next loop if certain condition occures then just use Continue;

    After Continue; immediately it will jump to next level of loop.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 11, 2011 3:14 PM

All replies

  • User-292625832 posted

    sorry but not getting what you want please elaborate your problem.

    Friday, March 11, 2011 3:00 PM
  • User451945003 posted

    When the IF construct qualifies I want to stop processing that Row data and start processing the next Row.  OR Alternatively jump to the bottom of the remaining code in the loop.  Is there a Jump to command in C#?

    Friday, March 11, 2011 3:05 PM
  • User-292625832 posted

    so you have written correct "continue;" this will start executing next iteration.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 11, 2011 3:08 PM
  • User-1412735316 posted

    use for loop instead of for each as below

    for (int count = 0; count < dtGCats.Rows.Count; count++)

          {

                DataRow row = dtGCats.Rows[count];
                prevCG_ID = holdCG_ID;
                tempCG_ID = Convert.ToInt16(row["CG_ID"]);
                //  Skipping past Excess products (over 4) in Category
                if ( (tempCG_ID == prevCG_ID) && counter1 >= 4);
                      {  
                        counter1++;
                        ExtraItem = 1;
                   //     continue;  ****** WANT TO GO TO NEXT ROW, not leave the loop


    you can use following statements.

    if(count+1< dtGCats.Rows.Count)   // checking whether a next record exists or not

    {

    DataRow previousrow=dtGCats.Rows[count-1];

    ...........do your coding..........

    }

                     }
               
                //  Load first row of new GLinks DataTable
                if (Convert.ToInt16(row["rnum"]) == 0)
                {
                    dr = GLinks.NewRow();
                    dr["rnum"] = row["rnum"];
                     . . . . . . . . . .

    Friday, March 11, 2011 3:09 PM
  • User-1412735316 posted

    I may miss understood your problem.

    If in a loop you want to do some sort of calculation with next values then again want to proceed the current loop then my previous solution is ok.

    If you just want to go next loop if certain condition occures then just use Continue;

    After Continue; immediately it will jump to next level of loop.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 11, 2011 3:14 PM
  • User451945003 posted

    sorry - foolish question.  Did not know about "goto"  I assume it will work.
    goto NextRow;  NextRow:

               if ( (tempCG_ID == prevCG_ID) && counter1 >= 4);
                      {  
                        counter1++;
                        ExtraItem = 1;
     
                    //    goto NextRow;
                      }

    Why does this goto statement negate all code below it?

     

    Friday, March 11, 2011 3:15 PM
  • User451945003 posted

    I am processing Data one row at a time.  This if statement determines that the row being looked at should NOT be added to the new DataTable.  So I want to immediately end processing of this row and move on to the next row in the foreach sequence.  Is there a "next" command for foreach?

    Friday, March 11, 2011 3:22 PM
  • User451945003 posted

    I thought it would be it appears to be ignoring the remaining row of data leave my GalleryByCat() routine completely.  I am checking again.

    Friday, March 11, 2011 3:25 PM
  • User451945003 posted

    As stated by Nirali I should be using 'continue'.  I must have issues in the code because it is acting as if the process is being abandoned.  Sorry for miscue.

    Friday, March 11, 2011 3:42 PM