[英]How to skip a specific position within a for each loop in c sharp?
List<string> liste = new List<String>
{
"A","B","C","D"
};
foreach (var item in liste)
{
System.Diagnostics.Debug.WriteLine(item.ToString());
}
for (int i = 0; i < liste.Count; i++)
{
if (i == 0)
continue;
System.Diagnostics.Debug.WriteLine(liste[i].ToString());
}
How do i skip a specific position in a foreach loop? 如何跳过foreach循环中的特定位置? I do not want to evaluate any values, but just skip the position x.
我不想评估任何值,只是跳过位置x。
It has to be a specific position. 它必须是一个特定的位置。 One could choose position 0 or maybe position 7.
可以选择位置0或位置7。
It is very easy to skip the first item in the list: 跳过列表中的第一项非常容易:
foreach(var item in list.Skip(1))
{
System.Diagnostics.Debug.WriteLine(item.ToString());
}
If you want to skip any other element at index n
, you could write this: 如果你想跳过索引
n
处的任何其他元素,你可以这样写:
foreach(var item in list.Where((a,b) => b != n))
{
System.Diagnostics.Debug.WriteLine(item.ToString());
}
In this example I use a lambda expression that takes two arguments: a
and b
. 在这个例子中,我使用一个带有两个参数的lambda表达式:
a
和b
。 Argument a
is the item itself, while argument b
is the index of the item. 参数
a
是项本身,而参数b
是项的索引。
The relevant pages on MSDN that describe these extension methods are: MSDN上描述这些扩展方法的相关页面是:
You could even write your own extension method that allows you to skip an element in a list: 您甚至可以编写自己的扩展方法,允许您跳过列表中的元素:
public static class MyEnumerableExtensions
{
public static IEnumerable<T> SkipAt<T>(this IEnumerable<T> list, int index)
{
var i = 0;
foreach(var item in list)
{
if(i != index)
yield return item;
i++;
}
}
}
This will allow you to write something like this to skip an item: 这将允许你写这样的东西来跳过一个项目:
foreach(var item in list.SkipAt(2))
{
System.Diagnostics.Debug.WriteLine(item.ToString());
}
A foreach
loop iterates over a collection that implements IEnumerable
. foreach
循环遍历实现IEnumerable
的集合。 The enumerator exposes the current item and a method to move onto the next item - it has no concept of an index. 枚举器公开当前项和移动到下一项的方法 - 它没有索引的概念。
You could always do: 你可以随时做:
var i = 0;
foreach (var item in liste) {
if (i++ == skip) continue;
Debug.WriteLine(item.ToString());
}
But this seems unnecessarily contrived. 但这似乎不必要地做作。 If you need an index, go with a
for
loop. 如果您需要索引,请使用
for
循环。
The other option is to remove the undesired item from the List
before iterating: 另一个选项是在迭代之前从
List
删除不需要的项:
foreach (var item in liste.Take(n-1).Union(liste.Skip(n))) {
Debug.WriteLine(item.ToString());
}
I love list's .ForEach, here's my take using @Elian's .SkipAt(n) and .ForEach: 我喜欢list's。ForEach,这是我使用@ Elian的.SkipAt(n)和.ForEach:
var list = new List<String> { "A", "B", "C", "D" };
list = list.SkipAt(1).ToList();
list.ForEach(Debug.WriteLine);
You should try using the enhanced version of the Where extension method that allows you to filter on item and index. 您应该尝试使用Where扩展方法的增强版本,该方法允许您过滤项目和索引。
Check the reference. 检查参考。 http://msdn.microsoft.com/en-us/library/bb549418.aspx
http://msdn.microsoft.com/en-us/library/bb549418.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Verbatim list");
List<string> list = new List<String> { "A","B","C","D" };
foreach (var item in list)
{
Console.WriteLine(item.ToString());
}
Console.WriteLine("Filtered list");
int itemToSkip = 2;
foreach (var item in list.Where((item, index) => index != itemToSkip))
{
Console.WriteLine(item.ToString());
}
Console.ReadKey();
}
}
}
This will give you the following output. 这将为您提供以下输出。
Verbatim list
A
B
C
D
Filtered list
A
B
D
To skip a position inside the foreach loop, one option is that you can skip the action inside the foreach loop by using an if statement, like 要跳过foreach循环内的一个位置,一个选项是你可以使用if语句跳过foreach循环内的动作,比如
foreach(var item in liste)
{
if (item != 'X')
{
//do something
}
}
But i am waiting for better solutions 但我在等待更好的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.