【发布时间】:2016-07-19 10:51:13
【问题描述】:
我有两个这样的字符串列表
list<string> OldlicenseList = new List<string>{"abcs", "rtets", "taya", "tctct"}; //sample data
list<string> subscriptionList = new List<string> {"udud", "ydyyd" , "tsts","tstst", "hghs"} //Sample data
所以我在下面这样做
foreach (var strOldLicense in OldlicenseList)
{
foreach (var subscriptionList in objSubscription.lstClsLicense)
{
newLicenseList.Add(subscriptionList.license_key);
isInserted = _objcertificate.UpdateClpInternalLicense(subscriptionKey, _objUserInfo.GetUserAcctId(), strOldLicense, subscriptionList.license_key, expiryDT);
if (!isInserted)
{
new Log().logInfo(method, "Unable to update the Subscription key." + certID);
lblErrWithRenewSubKey.Text = "Application could not process your request. The error has been logged.";
lblErrWithRenewSubKey.Visible = true;
return;
}
insertCount++;
if (insertCount >= 4)
{
break;
}
}
}
这里我需要将列表中的每个项目都传递给方法 UpdateClpInternalLicense 和第二个列表(subscriptionList)有 5 个项目,但我需要停止发送第 5 个项目到那个方法..
我尝试了上述方法,但问题是一旦内部循环完成..它将再次循环,因为第一个列表中有 4 个项目..
我需要将每个列表中的一项传递给该方法,直到 4 次迭代,在第 4 次迭代之后,我需要从两个循环中退出 ..
哪位大神可以提点意见,不胜感激..
非常感谢..
【问题讨论】:
-
嗯...只需在您的第一个 foreach 中添加另一个
if (insertCount >= 4) { break; }...? -
你试过用
for代替foreach吗? -
为什么不使用简单的 'for' 循环而不是 'foreach' 并运行尽可能多的迭代
-
我只能从外部列表(第一个列表)传递一个项目..但这不是我想要的......一旦我从外部列表传递了一个项目,它将进入第二个循环和第二个循环将从第一个列表中获取相同的项目,直到完成计数..
标签: c# .net list c#-4.0 foreach