You can do in two steps:
- Create a pair of value and index
- Group the values by
index/5
The snippet:
var groups = myList.Select((x,i) => new {X=x, I=i/5})
.GroupBy(xi => xi.I, xi => xi.X);
Example Program:
using System.Linq;
namespace BatchLinq
{
class Program
{
static void Main(string[] args)
{
var myList = Enumerable.Range(0, 23);
var groups = myList.Select((x,i) => new {X=x, I=i/5})
.GroupBy(xi => xi.I, xi => xi.X);
foreach (var group in groups)
System.Console.Out.WriteLine("{{ {0} }}", string.Join(", ", group));
}
}
}
The output is:
{ 0, 1, 2, 3, 4 }
{ 5, 6, 7, 8, 9 }
{ 10, 11, 12, 13, 14 }
{ 15, 16, 17, 18, 19 }
{ 20, 21, 22 }