C# || How To Split & Batch An Array/List/IEnumerable Into Smaller Sub-Lists Of N Size Using C#
The following is a module with functions which demonstrates how to split/batch an Array/List/IEnumerable into smaller sublists of n size using C#.
This generic extension function uses a simple for loop to group items into batches.
1. Partition – Integer Array
The example below demonstrates the use of ‘Utils.Extensions.Partition‘ to group an integer array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// Partition - Integer Array using Utils; // Declare array of integers var numbers = new int[] { 1987, 19, 22, 2009, 2019, 1991, 28, 31 }; // Split array into sub groups var numbersPartition = numbers.Partition(3); // Display grouped batches for (var batchCount = 0; batchCount < numbersPartition.Count; ++batchCount) { var batch = numbersPartition[batchCount]; Console.WriteLine($"Batch #{batchCount + 1}"); foreach (var item in batch) { Console.WriteLine($" Item: {item}"); } } // expected output: /* Batch #1 Item: 1987 Item: 19 Item: 22 Batch #2 Item: 2009 Item: 2019 Item: 1991 Batch #3 Item: 28 Item: 31 */ |
2. Partition – String List
The example below demonstrates the use of ‘Utils.Extensions.Partition‘ to group a list of strings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// Partition - String List using Utils; // Declare list of strings var names = new List<string>() { "Kenneth", "Jennifer", "Lynn", "Sole" }; // Split array into sub groups var namesPartition = names.Partition(2); // Display grouped batches for (var batchCount = 0; batchCount < namesPartition.Count; ++batchCount) { var batch = namesPartition[batchCount]; Console.WriteLine($"Batch #{batchCount + 1}"); foreach (var item in batch) { Console.WriteLine($" Item: {item}"); } } // expected output: /* Batch #1 Item: Kenneth Item: Jennifer Batch #2 Item: Lynn Item: Sole */ |
3. Partition – Custom Object List
The example below demonstrates the use of ‘Utils.Extensions.Partition‘ to group a list of custom objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
// Partition - Custom Object List using Utils; public class Part { public string PartName { get; set; } public int PartId { get; set; } } // Declare list of objects var parts = new List<Part>() { new Part() { PartName = "crank arm", PartId = 1234 }, new Part() { PartName = "chain ring", PartId = 1334 }, new Part() { PartName = "regular seat", PartId = 1434 }, new Part() { PartName = "banana seat", PartId = 1444 }, new Part() { PartName = "cassette", PartId = 1534 }, new Part() { PartName = "shift lever", PartId = 1634 } }; // Split array into sub groups var partsPartition = parts.Partition(4); // Display grouped batches for (var batchCount = 0; batchCount < partsPartition.Count; ++batchCount) { var batch = partsPartition[batchCount]; Console.WriteLine($"Batch #{batchCount + 1}"); foreach (var item in batch) { Console.WriteLine($" Item: {item.PartId} - {item.PartName}"); } } // expected output: /* Batch #1 Item: 1234 - crank arm Item: 1334 - chain ring Item: 1434 - regular seat Item: 1444 - banana seat Batch #2 Item: 1534 - cassette Item: 1634 - shift lever */ |
4. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 9, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.cs // Description: Handles general utility functions // ============================================================================ using System; using System.Collections.Generic; namespace Utils { public static class Extensions { /// <summary> /// Breaks a list into smaller sub-lists of a specified size /// </summary> /// <param name="source">An IEnumerable to split into smaller sub-lists</param> /// <param name="size">The maximum size of each sub-list</param> /// <returns>The smaller sub-lists of the specified size</returns> public static List<List<T>> Partition<T>(this IEnumerable<T> source, int size) { var result = new List<List<T>>(); List<T> batch = null; int index = 0; foreach (var item in source) { if (index % size == 0) { batch = new List<T>(); result.Add(batch); } batch.Add(item); ++index; } return result; } } }// http://programmingnotes.org/ |
5. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 9, 2021 // Taken From: http://programmingnotes.org/ // File: Program.cs // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ using System; using System.Diagnostics; using System.Collections.Generic; using Utils; public class Program { public class Part { public string PartName { get; set; } public int PartId { get; set; } } static void Main(string[] args) { try { // Declare array of integers var numbers = new int[] { 1987, 19, 22, 2009, 2019, 1991, 28, 31 }; // Split array into sub groups var numbersPartition = numbers.Partition(3); // Display grouped batches for (var batchCount = 0; batchCount < numbersPartition.Count; ++batchCount) { var batch = numbersPartition[batchCount]; Display($"Batch #{batchCount + 1}"); foreach (var item in batch) { Display($" Item: {item}"); } } Display(""); // Declare list of strings var names = new List<string>() { "Kenneth", "Jennifer", "Lynn", "Sole" }; // Split array into sub groups var namesPartition = names.Partition(2); // Display grouped batches for (var batchCount = 0; batchCount < namesPartition.Count; ++batchCount) { var batch = namesPartition[batchCount]; Display($"Batch #{batchCount + 1}"); foreach (var item in batch) { Display($" Item: {item}"); } } Display(""); // Declare list of objects var parts = new List<Part>() { new Part() { PartName = "crank arm", PartId = 1234 }, new Part() { PartName = "chain ring", PartId = 1334 }, new Part() { PartName = "regular seat", PartId = 1434 }, new Part() { PartName = "banana seat", PartId = 1444 }, new Part() { PartName = "cassette", PartId = 1534 }, new Part() { PartName = "shift lever", PartId = 1634 } }; // Split array into sub groups var partsPartition = parts.Partition(4); // Display grouped batches for (var batchCount = 0; batchCount < partsPartition.Count; ++batchCount) { var batch = partsPartition[batchCount]; Display($"Batch #{batchCount + 1}"); foreach (var item in batch) { Display($" Item: {item.PartId} - {item.PartName}"); } } } catch (Exception ex) { Display(ex.ToString()); } finally { Console.ReadLine(); } } static void Display(string message) { Console.WriteLine(message); Debug.Print(message); } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply