I have the following code:
class Program
{
static void Main(string[] args)
{
foreach (var item in GetEnumerable().Skip(100))
{
Console.WriteLine(item);
}
}
static IEnumerable<int> GetEnumerable(int? page = null, int limit = 10)
{
var currentPage = page ?? 1;
while (true)
{
Thread.Sleep(1000); // emulates slow retrieval of a bunch of results
for (int i = limit * (currentPage - 1); i < limit * currentPage; i++)
{
yield return i;
}
currentPage++;
}
}
}
I’d like to be able to use .Skip(n) to efficiently skip results I don’t need. So, for example, if I use Skip(100) and each request retrieves 10 items, the first 10 requests should be skipped entirely.
Is there a pattern I can use to achieve this?
You can create your own
IEnumerable<int>type and provide your own implementation ofSkip:You can then replace your
GetEnumerablewith: