I just realized it myself ...
The IEnumerable<T>.Where() method has an overload that accepts the index of the current element - exactly what the doctor ordered.
(new []{1,2,3,4,5}).Where((elem, idx) => idx % 2 == 0);
It will return
{1, 3, 5}
Update. To cover both my use case and Dan Tao's suggestion, let's also indicate what the first returned item should be:
var firstIdx = 1; var takeEvery = 2; var list = new []{1,2,3,4,5}; var newList = list .Skip(firstIdx) .Where((elem, idx) => idx % takeEvery == 0);
... will return
{2, 4}
Cristi diaconescu
source share