I'm trying to split a collection into an specific number of parts, I've taken some help seeying solutions over StackOverflow: Split a collection into `n` parts with LINQ?
This is my VB.Net translation from @Hasan Khan solution:
''' <summary>
''' Splits an <see cref="IEnumerable(Of T)"/> into the specified amount of secuences.
''' </summary>
Public Shared Function SplitIntoParts(Of T)(ByVal col As IEnumerable(Of T),
ByVal amount As Integer) As IEnumerable(Of IEnumerable(Of T))
Dim i As Integer = 0
Dim splits As IEnumerable(Of IEnumerable(Of T)) =
From item As T In col
Group item By item = Threading.Interlocked.Increment(i) Mod amount
Into Group
Select Group.AsEnumerable()
Return splits
End Function
And this my VB.Net translation of @manu08 solution:
''' <summary>
''' Splits an <see cref="IEnumerable(Of T)"/> into the specified amount of secuences.
''' </summary>
Public Shared Function SplitIntoParts(Of T)(ByVal col As IEnumerable(Of T),
ByVal amount As Integer) As IEnumerable(Of IEnumerable(Of T))
Return col.Select(Function(item, index) New With {index, item}).
GroupBy(Function(x) x.index Mod amount).
Select(Function(x) x.Select(Function(y) y.item))
End Function
The problem is that both functions returns a wrong result.
Because if I split a collection like this:
Dim mainCol As IEnumerable(Of Integer) = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim splittedCols As IEnumerable(Of IEnumerable(Of Integer)) =
SplitIntoParts(col:=mainCol, amount:=2)
Both functions gives this result:
1: { 1, 3, 5, 7, 9 }
2: { 2, 4, 6, 8, 10 }
Instead of these secuences:
1: { 1, 2, 3, 4, 5 }
2: { 6, 7, 8, 9, 10 }
What I'm doing wrong?.