0

I have a dictionary that looks like this:

Dictionary<string, int> supplierStockLevels = new Dictionary<string, int>()
{
    { "ABCD1", 1},
    { "ABCD2", 2},
    { "ABCD3", 2},
    { "ABCD4", 1},
    { "ABCD5", 3}
};

I want to pluck a chunk of 2 records at a time out of this dictionary and process it.

In PHP, I normally do something like this:

$supplierStockLevels = 
[
    'ABCD1' => 1,
    'ABCD2' => 2,
    'ABCD3' => 2,
    'ABCD4' => 1,
    'ABCD5' => 3
];

$preserveKeys = true;
$batchSize = 2;

foreach (array_chunk($supplierStockLevels, $batchSize, $preserveKeys) as $batch)
{
    // Do something
    var_dump($batch);
}

Outputs:

enter image description here

How can I achieve the same C# .NET (4.5.x)?

Latheesan
  • 23,247
  • 32
  • 107
  • 201
  • 2
    `Dictionary` is enumerable of Key/Value pairs - so answers that "batch enumerable" cover your case like http://stackoverflow.com/questions/15414347/how-to-loop-through-ienumerable-in-batches. – Alexei Levenkov Feb 26 '16 at 19:10
  • Thanks for that link; I have solved it like this: http://pastebin.com/ZHAxZfRb – Latheesan Feb 26 '16 at 19:22

0 Answers0