I got a list of data like bellow. Here I already added 50 demo items in list. Now I want to make a chunk of list splitted by 10 items in each chunk. How can I do it? I already tried like bellow using while
loop and GroupBy
but I am not getting idea what's the best way to do it? All I need is from this 50 items it should be make a new list which will GroupBy
10 items.
Model:
class Data
{
public int Id { get; set; }
public string Text { get; set; }
public static List<Data> data { get; set; } = new List<Data>();
}
Main Program:
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 50; i++)//seeding 50 example data to global class
{
Data.data.Add(new Data
{
Id = i,
Text = null,
});
}
var chunkList = new List<Data>();//i need a chunk of list devided by 10 items in each chunk
int count = 0;
foreach (var item in Data.data)
{
do
{
chunkList.AddRange(chunkList.GroupBy(u => u.Id).Select(grp => grp.ToList()).ToList());//this logic is wrong
count++;
} while (count == 10);
}
Console.WriteLine("");
}