如何从 List<T> 中获取每第 n 个项目? [英] How can I get every nth item from a List&lt;T&gt;?

查看:30
本文介绍了如何从 List<T> 中获取每第 n 个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .NET 3.5 并且希望能够从列表中获取每个 *n*th 项.我并不担心它是使用 lambda 表达式还是 LINQ 实现的.

I'm using .NET 3.5 and would like to be able to obtain every *n*th item from a List. I'm not bothered as to whether it's achieved using a lambda expression or LINQ.

编辑

看起来这个问题引起了很多争论(这是一件好事,对吧?).我学到的主要事情是,当您认为自己知道做某事的所有方法时(即使像这样简单),请再想一想!

Looks like this question provoked quite a lot of debate (which is a good thing, right?). The main thing I've learnt is that when you think you know every way to do something (even as simple as this), think again!

推荐答案

return list.Where((x, i) => i % nStep == 0);

这篇关于如何从 List<T> 中获取每第 n 个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆