【问题标题】:List<T> moving to the next elementList<T> 移动到下一个元素
【发布时间】:2012-08-30 14:18:31
【问题描述】:

我搜索了 SO 并找到了一些帖子,但无法让它们工作。

问题:如何循环到列表集合中的下一项(custLoginHist[1] 等)?

    List<eCommCustomer.oCustomer> custLoginHist = new List<eComm.oCustomer>();
    eCommCustomerDAL.GetCustomerPrevLogin(custLoginHist, oCust);

    if (custLoginHist.Count > 0)
    {
        eCommSecurityFactory oSecFactory = new eCommSecurityFactory();
        if (oCust.CustHash == oSecFactory.CreateHash(custLoginHist[0].CustSalt, custLoginHist[0].CustHash))
        {
            //Password has been used before;
            return false;
        }
        else
        {
            // Valid password;
            return true;
        }
    }
    return true;
}

【问题讨论】:

  • 我很困惑。你想完成什么?
  • 你只是想遍历集合吗?
  • @DavidB 移至 custLoginHist 中的下一项
  • 正如其他人指出的那样,由于它是一个实现 IEnumerable 的 List,因此您可以在其上使用 foreach。无需使用索引器或 for 循环。

标签: c# asp.net collections


【解决方案1】:
foreach(eCommCustomer.oCustomer cust in custLoginHist)
{
  //Do something with cust here.
}

或者:

for(int i = 0; i != custLoginHist.Count; ++i)
{
  eCommCustomer.oCustomer cust = custLoginHist[i];
  //Do something with cust here.
}

在这种情况下,我们希望对任何单个匹配项返回 false,否则返回 true,因此:

foreach(eCommCustomer.oCustomer cust in custLoginHist)
  if(oCust.CustHash == oSecFactory.CreateHash(custLoginHist[0].CustSalt, custLoginHist[0].CustHash)
    return false;
return true;//if we reached here, no matches.

不过,这是个坏主意,因为您让闯入系统变得更容易了。如果我尝试将我的密码设置为某个值,而你拒绝了,我现在知道你的一个用户使用了该密码。你最好让这种情况发生,尽管你可能应该通过质量检查来阻止一些更有可能的违规者(“password”、“password1”等)。

【讨论】:

    【解决方案2】:
    List<eCommCustomer.oCustomer> custLoginHist = new List<eComm.oCustomer>();
    eCommCustomerDAL.GetCustomerPrevLogin(custLoginHist, oCust);
    
    foreach (var custLogin in custLoginHist)
    {
        eCommSecurityFactory oSecFactory = new eCommSecurityFactory();
        if (oCust.CustHash == oSecFactory.CreateHash(custLogin.CustSalt, custLogin.CustHash))
        {
            //Password has been used before;
            return false;
        }
    }
    return true;
    

    尝试这样的事情,也许您必须自定义您的退货声明,但它应该让您了解它是如何工作的。

    【讨论】:

    • 这实际上不会遍历整个列表。它将在一次迭代后从函数返回,因此只会检查第一个元素。
    • 没错,我稍微编辑了我的答案,但它也不是 100% 操作想要完成的。
    【解决方案3】:
    foreach(var item in yourList)
    {
       //Iterate;
    }
    

    如果你想休息,你可以使用:break;

    如果你想完成你可以使用 : continue;

    【讨论】:

      【解决方案4】:

      List&lt;T&gt; 实现了IEnumerable&lt;T&gt;,所以你可以只使用foreach,或者如果你能够在循环中编辑T,你可以使用for

      foreach(var item in custLoginHist)
      {
      
      }
      

      或者

      for (int i = 0; i < custLoginHist.Count; i++)
      {
      
      }
      

      那么如果你需要在循环完成之前退出循环(比如你有一个条件为真,你可以直接使用break;退出循环,或者你可以在循环中使用return如果你想返回一个值。

      【讨论】:

        【解决方案5】:

        您可以为此循环。例如foreachfor

        foreach (var custLogin in custLoginHist)
        {
            eCommSecurityFactory oSecFactory = new eCommSecurityFactory();
            if (oCust.CustHash == oSecFactory.CreateHash(custLogin.CustSalt, custLogin.CustHash))
            {
                //Password has been used before;
                return false;
            }
            else
            {
                // Valid password;
                return true;
            }
        }
        

        【讨论】:

          【解决方案6】:
          List<eCommCustomer.oCustomer> custLoginHist = new List<eComm.oCustomer>();
          eCommCustomerDAL.GetCustomerPrevLogin(custLoginHist, oCust);
          
          
          
          return custLoginHist.Select(c=>oSecFactory.CreateHash(c.CustSalt,c.CustHash))
                              .Any(x=>x==oCust.CustHash)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-02-01
            • 1970-01-01
            • 2020-05-23
            • 2021-04-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多