简体   繁体   English

LINQ:如何跳过一个然后接受序列的其余部分

[英]LINQ: How to skip one then take the rest of a sequence

i would like to iterate over the items of a List<T> , except the first, preserving the order. 我想迭代List<T> ,除了第一项,保留订单。 Is there an elegant way to do it with LINQ using a statement like: 有没有一种优雅的方法可以使用如下语句对LINQ执行此操作:

foreach (var item in list.Skip(1). TakeTheRest() ) {.... foreach(列表中的var项目.kip(1) .TakeTheRest() ){....

I played around with TakeWhile , but was not successful. 我和TakeWhile一起TakeWhile ,但没有成功。 Probably there is also another, simple way of doing it? 可能还有另一种简单的方法吗?

From the documentation for Skip : Skip的文档:

Bypasses a specified number of elements in a sequence and then returns the remaining elements. 绕过序列中指定数量的元素,然后返回其余元素。

So you just need this: 所以你只需要这个:

foreach (var item in list.Skip(1))

Just do: 做就是了:

foreach (var item in input.Skip(1))

There's some more info on the MSDN and a simple example that's downloadable here 有关MSDN的更多信息以及可在此处下载的简单示例

不是吗......

foreach (var in list.Skip(1).AsEnumerable())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM