Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#循环限制为50次_C#_Loops - Fatal编程技术网

C#循环限制为50次

C#循环限制为50次,c#,loops,C#,Loops,如何将循环限制在50以下,使其在到达第51项时停止 foreach (ListViewItem lvi in listView.Items) { } 谢谢使用for循环 for(int index = 0; index < collection.Count && index < 50; index++) { collection[index]; } for(int index=0;index

如何将循环限制在50以下,使其在到达第51项时停止

foreach (ListViewItem lvi in listView.Items)
{

}
谢谢

使用for循环

for(int index = 0; index < collection.Count && index < 50; index++)
{
   collection[index];
}
for(int index=0;index
或者因为它是列表视图项

foreach (ListViewItem lvi in listView.Items) {
  // do code here
  if (lvi.Index == 49) break;
}
根据使用Linq

根据使用For循环

//循环遍历集合,最多50个或项数
对于(int i=0;i
好吧,foreach可能不是最好的解决方案,但如果必须:

int ctr = 0;
foreach (ListViewItem lvi in listView.Items) {
    ctr++;
    if (ctr == 50) break;

    // do code here

}
注意:for循环通常比使用foreach遍历集合要轻。

最好使用for循环:

// loop through collection to a max of 50 or the number of items
for(int i = 0; i < listView.Items.Count && i < 50; i++){
    listView.Items[i]; //access the current item

}
//循环遍历集合,最多50个或项数
对于(int i=0;i
如果仍要使用foreach循环,请尝试以下操作:

int counter = 0;
foreach (ListViewItem lvi in listView.Items) 
{
  counter++;
  if ( counter == 50 )
  {
   break;
  }
}

我会像charles建议的那样使用for循环,而不是带有索引检查的foreach。当您需要跟踪当前迭代时,使用for循环的意图更为明显

for (int i = 0; i < listView.Items.Count && i < 50; ++i)
{
    //do something with listView.Items[i]
}
for(int i=0;i
inti=50;
foreach(T在TList中)
{
if(i--
for(int index=0,limit=Math.Min(50,collection.Count);index
for(int i=0;i<50&&i
人们已经给出了很多例子,在这个特定的例子中,自从ListView.Items是一个索引集合以来,老式的for循环可能是最好的。如果它像IEnumerable一样,不能使用Items[i],那么你就必须像其他例子一样使用外部计数器变量。

简单使用Linq

foreach (ListViewItem lvi in listView.Items.Take(50)) {

}
从MSDN文档中:

列举 源并生成元素,直到计数 元素已经产生或来源 不包含更多元素

如果计数小于或等于 零,源未枚举,并且 空(IEnumerable)是 返回


for循环可以工作,但是您仍然可以像这样设置名为lvi的ListViewItem

int maxItems = listViewItems.Count > 50 ? 50 : listViewItems.Count;
for(int counter = 0; counter < maxItems; counter ++)
{ 
    ListViewItem lvi = listView.Items[counter];

    // Rest of your code here will stile work

}
int maxItems=listViewItems.Count>50?50:listViewItems.Count;
对于(int counter=0;counter
使用LINQ,这可以实现为:

foreach (ListViewItem lvi in listView.Items.Take(50))
{
  // do stuff
}


Take(n)返回前n个元素,如果可用项少于n,则返回所有元素。

如果集合没有50项,则此操作将失败。此操作仍然不起作用。如果集合中的项少于50项,则根本不会执行。它将起作用。for循环只不过是一个奇特的while循环。(而您的“计数”小于50项。)(0-49)执行循环。s/=/==/它可能会被关闭一次。但是你得到了正确的中断。我在这里添加了//do代码,这样它将在处理第50条记录结束时退出,以澄清:)这远没有使用Linq处理对象那么好…请参阅@Lukedoff的答案below@smiller-不整洁,但功能符合规范:)@Jade M-NPChecking listView.Items.IndexOf(lvi)在每次迭代中相对于维护一个独立的计数器来说是非常昂贵的。如果您的关键标准只是获取50个项目,那么LINQ to objects Take(50)方法是最快的方法。用于(int i=0;iint i = 50; foreach(T t in TList) { if(i-- <= 0) break; code; // or: i--; if(i<=0) break; }
for(int index = 0, limit = Math.Min(50, collection.Count); index < limit; index++)
{
   collection[index];
}
for (int i = 0; i < 50 && i < listView.Items.Count; ++i)
{
    ListViewItem item = listView.Items[i];
}
foreach (ListViewItem lvi in listView.Items.Take(50)) {

}
int maxItems = listViewItems.Count > 50 ? 50 : listViewItems.Count;
for(int counter = 0; counter < maxItems; counter ++)
{ 
    ListViewItem lvi = listView.Items[counter];

    // Rest of your code here will stile work

}
foreach (ListViewItem lvi in listView.Items.Take(50))
{
  // do stuff
}