【问题标题】:loop through two lists at the same time and passing each item to method同时循环遍历两个列表并将每个项目传递给方法
【发布时间】: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 &gt;= 4) { break; }...?
  • 你试过用for代替foreach吗?
  • 为什么不使用简单的 'for' 循环而不是 'foreach' 并运行尽可能多的迭代
  • 我只能从外部列表(第一个列表)传递一个项目..但这不是我想要的......一旦我从外部列表传递了一个项目,它将进入第二个循环和第二个循环将从第一个列表中获取相同的项目,直到完成计数..

标签: c# .net list c#-4.0 foreach


【解决方案1】:

你可以这样做:

var query =
    from strOldLicense in OldlicenseList
    from subscriptionList in objSubscription.lstClsLicense
    select new { strOldLicense, subscriptionList };

foreach (var x in query)
{
    newLicenseList.Add(x.subscriptionList.license_key);
    isInserted = _objcertificate.UpdateClpInternalLicense(subscriptionKey, _objUserInfo.GetUserAcctId(), x.strOldLicense, x.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;
    }
}

这会将它归结为您正在迭代的单个列表,因此break 将起作用。

【讨论】:

  • 每次我们循环查询时这个变量只持有第一个许可证时,我们都会遇到这个“x.strOldLicense”的问题..你能帮忙吗..
  • @EnigmaState - 尝试遍历整个 query 枚举。应该没问题。我猜insertCount 在获得下一个x.strOldLicense 值之前会超过4
  • no in this statement isInserted = _objcertificate.UpdateClpInternalLicense(subscriptionKey, _objUserInfo.GetUserAcctId(), x.strOldLicense, x.subscriptionList.license_key, expiryDT);只有当我们遍历查询结果时,这个变量“x.strOldLicense”才会始终持有第一个许可证\
  • @EnigmaState - 是的,但这不是我要求你做的。遍历整个 query 枚举并检查 - 不要在 foreach 中运行代码 - 而是放入 Console.WriteLine(...),这样您就可以绝对检查 x 的每个值。如果它们都相同,则 OldlicenseList 必须只有一个值。
  • @EnigmaState - 你过得怎么样?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-15
  • 2019-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
相关资源
最近更新 更多