如何使用c#走出内部foreach循环 [英] how to come out of the inner foreach loop using c#
本文介绍了如何使用c#走出内部foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我实现了一个方法,使用foreach循环作为外部循环和内部循环,根据某些条件插入记录。请看下面的代码...
Hi, I implemented a method to insert the record based on some condition using foreach loop as outer as well as inner loop. please have a look into below code...
try
{
dbManager = new DBManager(DataProvider.SqlServer, Constants.ConnectionString);
dbManager.ConnectionString = Constants.ConnectionString;
dbManager.ProviderType = DataProvider.SqlServer;
dbManager.Open();
dbManager.Command.CommandTimeout = Constants.CommandTimeOut;
dbManager.Command.CommandText = "DELETE dbo.PAYROLL_EMPLOYEE_LEAVES_SETUP WHERE EMP_NO='" + iObjects.EMP_NO + "'";
dbManager.Command.Connection = dbManager.Connection;
int _id = dbManager.Command.ExecuteNonQuery();
if (!string.IsNullOrWhiteSpace(leavetypeid))
{
string[] arrRay = leavetypeid.Split(',');
foreach (var id in arrRay)
{
if (!string.IsNullOrWhiteSpace(id))
{
iObjects.LEAVE_TYPE_ID = Convert.ToInt32(id);
//for leave types
if (!string.IsNullOrWhiteSpace(leavetypeid))
{
string[] aray_ltype = leavetype.Split(',');
foreach (var id_ltype in aray_ltype)
{
if (!string.IsNullOrWhiteSpace(id_ltype))
{
iObjects.TOTAL_LEAVES = float.Parse(id_ltype);
}
}
}
sql = @"SET NOCOUNT ON INSERT INTO dbo.PAYROLL_EMPLOYEE_LEAVES_SETUP
(EMP_NO ,LEAVE_TYPE_ID,TOTAL_LEAVES,IS_ACTIVE,IS_DELETED,USER_ID,IP,CHECK_SUM,TIME_STAMP,SYSTEM_ID,SITE_ID)
VALUES ('{0}',{1},{2},'{3}', 0 ,'{4}', '{5}','{6}',GETDATE(),{7},'{8}') ";
//
sql = string.Format(sql,
Helper.IsNull(iObjects.EMP_NO)
, Helper.NullIf<int>(iObjects.LEAVE_TYPE_ID)
, Helper.NullIf<float>(iObjects.TOTAL_LEAVES)
, Helper.NullIf<bool>(iObjects.IS_ACTIVE)
, Helper.IsNull(iObjects.USER_ID)
, Helper.IsNull(iObjects.IP)
, Helper.IsNull(iObjects.CHECK_SUM)
, Helper.NullIf<int>(iObjects.SITE_ID)
, Helper.IsNull(iObjects.SYSTEM_ID));
//
sql = sql.Replace("''", "NULL").Replace("'NULL'", "NULL").Replace("NNULL", "NULL");
dbManager.Command.CommandTimeout = Constants.CommandTimeOut;
dbManager.Command.CommandText = sql;
dbManager.Command.Connection = dbManager.Connection;
int _Count = dbManager.Command.ExecuteNonQuery();
}
}
}
}
catch (Exception Exp)
{
dbManager.Command.Transaction.Rollback();
throw Exp;
}
推荐答案
这个函数太长了,并且做了太多不同的事情。将其拆分为适当的较小函数,解决方案自动生成。
That function is faaaaar too long, and does too many different things. Split it into appropriate smaller functions, and the solution arises automatically.
if (!string.IsNullOrWhiteSpace(leavetype))
{
string[] aray_ltype = leavetype.Split(',');
foreach (var id_ltype in aray_ltype)
{
if (!string.IsNullOrWhiteSpace(id_ltype))
{
iObjects.TOTAL_LEAVES = float.Parse(id_ltype);
}
}
}
可以重构为
can be refactored to
private bool TryGetLeaveTypeId(string leaveType, out float leaveTypeId)
{
leaveTypeId = 0f;
if (!string.IsNullOrWhiteSpace(leavetype))
{
string[] aray_ltype = leavetype.Split(',');
foreach (var id_ltype in aray_ltype)
{
if (!string.IsNullOrWhiteSpace(id_ltype))
{
leaveTypeID = float.Parse(id_ltype);
}
}
}
return leaveTypeID != 0;
}
然后插入你的函数等。
从你的问题,你正在寻找break
https://msdn.microsoft.com/en-us/ library / ttw7t8t6.aspx [ ^ ]
https://msdn.microsoft.com/en-us /library/adbctzc4.aspx [ ^ ]
但是正如其他评论所述,你的代码可能还有其他问题。
要理解你的代码为什么不做你想要什么,你应该使用调试器一步一步地执行你的代码,检查变量并查看它为什么这样做。
From your question, you are looking forbreak
https://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx[^]
https://msdn.microsoft.com/en-us/library/adbctzc4.aspx[^]
But as other comments state, your code may have other problems.
To understand why your code don't do what you want, you should use a debugger to follow step by step your code, inspect variable and see why it is doing what it does.
你好,
我们可以通过设置标志来实现这一点。以下是代码
Hi,
We can achieve this by setting a flag. Below is the code
List<string> test=new List<string>();
test.Add("Hi");
test.Add("Hello");
foreach (string item in test)
{
foreach (string item2 in test)
{
if(//Your condition here)
{
flag = true;
break;
}
}
if (flag) break;//if true exit from outer loop as well else continue
}
我希望这可以解决你的问题..
谢谢,
-Siva
I hope this resolves your issue..
Thanks,
-Siva
这篇关于如何使用c#走出内部foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文