int maxSize = 4;
int[] Ids = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (int i = 0; i < Math.Ceiling((double) Ids.Count() / (double)maxSize) ; i++)
{
Console.WriteLine("\nIteration " + i.ToString());
int[] brokenIDs = Ids.Skip(i * maxSize).Take(maxSize).ToArray();
foreach (int j in brokenIDs)
{
Console.Write(j.ToString());
}
}
Or another way
maxSize = 3;
Ids = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12 ,13};
for (int i = 0; i < Math.Ceiling((double) Ids.Count() /(double) maxSize) ; i++)
{
Console.WriteLine("\nIteration " + i.ToString());
for(int j = 0;j < maxSize && (i * maxSize) + j < Ids.Count();j++)
{
Console.Write(Ids[(i*maxSize) + j]);
}
}
Something more generic
void Main()
{
var arr = new int[]{1,2,3,4,5,6,7,8,9,0};
DotIt
}
private static IEnumerable< IEnumerable < T > > DotIt< T > (IEnumerable < T > collection, int size)
{
IEnumerable
int i = 0;
while(true)
{
returnCol = collection.Skip(i++ * size).Take(size);
if(returnCol.Count() != 0)
yield return returnCol;
else
yield break;
}
}