切换嵌套的Foreach循环。如何走出某种条件。 [英] Switch inside nested Foreach loops. how to get out on certain condtion.

查看:73
本文介绍了切换嵌套的Foreach循环。如何走出某种条件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一种情况,我被困了



我有一个开关盒内置两个嵌套的每个循环。如果开关盒是真的,我想离开内循环。





Hello i have a situation where i am stuck

i have a switch case inside two nested for each loops. i want to get out of the inner loop if switch case is true.


foreach(DataRow dr in dt.Rows)
  {
      foreach (DataColumn dc in dt.Columns) 
       {
           switch (dc.Ordinal)
                  {
                     case 0:
                           {
                            if( string.IsNullOrEmpty(dr[dc].ToString()))
                               {
                                 continue;
                               }  
                           }
                   }
       }
  }







我需要做的是如果switch case为true,那么我需要输出DataColumn循环并转到dataRow循环。但是继续只是再次带我到dc循环。




What i need to do is if the switch case is true then i need to getout of the DataColumn loop and go to dataRow loop. But continue just takes me to dc loop again.

推荐答案

bool exitLoop;
foreach(DataRow dr in dt.Rows)
{
    foreach (DataColumn dc in dt.Columns)
     {
         switch (dc.Ordinal)
                {
                   case 0:
                         {
                          if( string.IsNullOrEmpty(dr[dc].ToString()))
                             {
                               exitLoop = true;
                               break;
                             }
                         }
                 }
           if (exitLoop) break;
           
     }
   
}






使用以下样本



Hi,

use below sample

bool isExists=false;
foreach(DataRow dr in dt.Rows)
  {
      foreach (DataColumn dc in dt.Columns)
       {
           switch (dc.Ordinal)
                  {
                     case 0:
                           {
                            if( string.IsNullOrEmpty(dr[dc].ToString()))
                               {
                                   isExists=true;
                                   break;
                               }
                           }
                   }
       }
  }





继续,声明将继续下一个声明,而不是更好地使用break;它会打破当前循环并为您提供结果集。



Continue, statement will continue the next statement, instead of that better to use break; it's breaks the current looping and give you the result set.


这篇关于切换嵌套的Foreach循环。如何走出某种条件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆