Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ: take a sequence of elements from a collection

Tags:

c#

.net

linq

I have a collection of objects and need to take batches of 100 objects and do some work with them until there are no objects left to process.

Instead of looping through each item and grabbing 100 elements then the next hundred etc is there a nicer way of doing it with linq?

Many thanks

like image 441
Bob Avatar asked Nov 28 '22 12:11

Bob


1 Answers

static void test(IEnumerable<object> objects)
{
    while (objects.Any())
    {
        foreach (object o in objects.Take(100))
        {
        }
        objects = objects.Skip(100); 
    }
}

:)

like image 190
Andrey Avatar answered Dec 17 '22 08:12

Andrey