C# foreach string in array process in groups of X amout -
this question has answer here:
- how loop through ienumerable in batches 7 answers
i trying iterate through array , process every x amount (in case 90), move go though loop until array has been exhausted. achieved if array fixed amount, sadly not.
//test range of 90 total tag collection private void testexcelrange(string[] tagcollection) { string delltag = null; int maxgroupammount = 90; foreach(string singletag in tagcollection) { //process in groups of 90 -- maxgroupammount if (singletag != "none") { delltag += singletag+ "|"; } //after 90 process again until tagcollection complete } }
here method splits list or array chunks of size:
public ienumerable<ienumerable<t>> getchunks<t>(ienumerable<t> elements, int size) { var list = elements.tolist(); while (list.count > 0) { var chunk = list.take(size); yield return chunk; list = list.skip(size).tolist(); } }
you can process array this:
private void testexcelrange(string[] tagcollection) { string delltag = null; int maxgroupamount = 90; var chunks = getchunks(tagcollection, maxgroupamount); foreach (ienumerable<string> chunk in chunks) { //process in groups of 90 } }
Comments
Post a Comment