VB.NET || How To Split & Batch An Array/List/IEnumerable Into Smaller Sub-Lists Of N Size Using VB.NET
The following is a module with functions which demonstrates how to split/batch an Array/List/IEnumerable into smaller sublists of n size using VB.NET.
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.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 |
' Partition - Integer Array Imports Utils ' Declare array of integers Dim numbers = New Integer() {1987, 19, 22, 2009, 2019, 1991, 28, 31} ' Split array into sub groups Dim numbersPartition = numbers.Partition(3) ' Display grouped batches For batchCount = 0 To numbersPartition.Count - 1 Dim batch = numbersPartition(batchCount) Debug.Print($"Batch #{batchCount + 1}") For Each item In batch Debug.Print($" Item: {item}") Next Next ' 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.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 |
' Partition - String List Imports Utils ' Declare list of strings Dim names = New List(Of String) From { "Kenneth", "Jennifer", "Lynn", "Sole" } ' Split array into sub groups Dim namesPartition = names.Partition(2) ' Display grouped batches For batchCount = 0 To namesPartition.Count - 1 Dim batch = namesPartition(batchCount) Debug.Print($"Batch #{batchCount + 1}") For Each item In batch Debug.Print($" Item: {item}") Next Next ' 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.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 |
' Partition - Custom Object List Imports Utils Public Class Part Public Property PartName As String Public Property PartId As Integer End Class ' Declare list of objects Dim parts = New List(Of Part) From { New Part With { .PartName = "crank arm", .PartId = 1234 }, New Part With { .PartName = "chain ring", .PartId = 1334 }, New Part With { .PartName = "regular seat", .PartId = 1434 }, New Part With { .PartName = "banana seat", .PartId = 1444 }, New Part With { .PartName = "cassette", .PartId = 1534 }, New Part With { .PartName = "shift lever", .PartId = 1634 }} ' Split array into sub groups Dim partsPartition = parts.Partition(4) ' Display grouped batches For batchCount = 0 To partsPartition.Count - 1 Dim batch = partsPartition(batchCount) Debug.Print($"Batch #{batchCount + 1}") For Each item In batch Debug.Print($" Item: {item.PartId} - {item.PartName}") Next Next ' 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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 15, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils ''' <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> <Runtime.CompilerServices.Extension()> Public Function Partition(Of T)(source As IEnumerable(Of T), size As Integer) As List(Of List(Of T)) Dim result = New List(Of List(Of T)) Dim batch As List(Of T) = Nothing For index = 0 To source.Count - 1 If index Mod size = 0 Then batch = New List(Of T) result.Add(batch) End If batch.Add(source(index)) Next Return result End Function End Module End Namespace ' 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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 15, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Imports Utils Public Module Program Public Class Part Public Property PartName As String Public Property PartId As Integer End Class Sub Main(args As String()) Try ' Declare array of integers Dim numbers = New Integer() {1987, 19, 22, 2009, 2019, 1991, 28, 31} ' Split array into sub groups Dim numbersPartition = numbers.Partition(3) ' Display grouped batches For batchCount = 0 To numbersPartition.Count - 1 Dim batch = numbersPartition(batchCount) Display($"Batch #{batchCount + 1}") For Each item In batch Display($" Item: {item}") Next Next Display("") ' Declare list of strings Dim names = New List(Of String) From { "Kenneth", "Jennifer", "Lynn", "Sole" } ' Split array into sub groups Dim namesPartition = names.Partition(2) ' Display grouped batches For batchCount = 0 To namesPartition.Count - 1 Dim batch = namesPartition(batchCount) Display($"Batch #{batchCount + 1}") For Each item In batch Display($" Item: {item}") Next Next Display("") ' Declare list of objects Dim parts = New List(Of Part) From { New Part With { .PartName = "crank arm", .PartId = 1234 }, New Part With { .PartName = "chain ring", .PartId = 1334 }, New Part With { .PartName = "regular seat", .PartId = 1434 }, New Part With { .PartName = "banana seat", .PartId = 1444 }, New Part With { .PartName = "cassette", .PartId = 1534 }, New Part With { .PartName = "shift lever", .PartId = 1634 }} ' Split array into sub groups Dim partsPartition = parts.Partition(4) ' Display grouped batches For batchCount = 0 To partsPartition.Count - 1 Dim batch = partsPartition(batchCount) Display($"Batch #{batchCount + 1}") For Each item In batch Display($" Item: {item.PartId} - {item.PartName}") Next Next Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' 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