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

时间秒杀一切 提交于 2019-12-09 13:58:58

问题


i would like to iterate over the items of a List<T>, except the first, preserving the order. Is there an elegant way to do it with LINQ using a statement like:

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

I played around with TakeWhile , but was not successful. Probably there is also another, simple way of doing it?


回答1:


From the documentation for 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))



回答2:


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




回答3:


Wouldn't it be...

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


来源:https://stackoverflow.com/questions/2431744/linq-how-to-skip-one-then-take-the-rest-of-a-sequence

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!