c# - Splitting a list or collection into chunks -


this question has answer here:

i have program read contents of file sort of list or array. list/array contain number of items. need split smaller groups, of 50 items each , processing on each item in each group.

list<string> stufffromfile = new list<string>();  while ((line = filereader.readline()) != null)       {            stufffromfile.add(line);       } 

i have been looking through examples online how chunk stuff honest, don't understand examples , of them seem overly complex. need simple chunk/split/break original list of items groups of 50 , let me iterate through each item in each group until processing complete.

the total number of items read in not number can divide evenly 50 last group may contain less 50 items, still need processed rest.

can here? sounds should simple don't know how it. i've seen example using linq don't understand either.

here's extension method work list , size chunks.

public static list<list<t>> splitlist<t>(this list<t> me, int size = 50) {     var list = new list<list<t>>();     (int = 0; < me.count; += size)         list.add(me.getrange(i, math.min(size, me.count - i)));     return list; }  

use this:

list<list<string>> chunksof50 = stufffromfile.splitlist(); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -