I'm a bit OCD about my code and wondered how other people structure the following sample control flow. I haven't found anything that passes my "pretty" code test.
var records = repo.GetRecords(batch:10);
while(records.Any())
{
var processed = ProcessRecords(records);
repo.DeleteRecords(processed);
records = repo.GetRecords(batch:10);
}
Thanks
Similar to @John Kugleman's above, but using while rather than for.
while (true)
{
var records = repo.GetRecords(batch:10);
if (!records.Any()) break;
var processed = ProcessRecords(records);
repo.DeleteRecords(processed);
}
You can also find questions like this:
Split List into Sublists with LINQ
That ask how to "chunkify" a sequence of items. That technique might or might not apply to your situation.
Assuming a Chunk extension method (similar to @TickleMeElmo's answer or Split from @JaredPar's answer), your code might look like this:
foreach (var chunk in repo.GetRecords().Chunk(10))
{
var processed = ProcessRecords(chunk);
repo.DeleteRecords(processed);
}
This idiom might not work that well if you already have some sort of "chunkification" built into to your repository.