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

发布于 2024-08-24 20:39:41 字数 252 浏览 3 评论 0原文

我想迭代 List 的项目(第一个除外),并保留顺序。有没有一种优雅的方法可以使用 LINQ 使用如下语句来做到这一点:

foreach(list.Skip(1).TakeTheRest()中的var item) {....

我尝试了 TakeWhile ,但没有成功。也许还有另一种简单的方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴情 2024-08-31 20:39:41

跳过的文档中:

绕过序列中指定数量的元素,然后返回剩余元素。

所以你只需要这个:

foreach (var item in list.Skip(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))
遥远的绿洲 2024-08-31 20:39:41

就这样做:

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

Microsoft Learn

Just do:

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

There's some more info on Microsoft Learn.

A君 2024-08-31 20:39:41

岂不是……

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

Wouldn't it be...

foreach (var in list.Skip(1).AsEnumerable())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文