Q:

Split datatable into n datatables

I need to dynamically split a datatable into multiple datatables and the number of subsequent datatables will vary. The end user will enter a value and this will determine the number of datatables that will be derived from the original. For example: the user enters 10, the original dt has 20 rows. There will be 10 dt's with 2 rows each created. However, if the original dt has 11 rows, there would be 9 dt's with 1 row and 1 dt with 2 rows created. How can I accomplish this in vb.net without hardcoding a bunch if rules? I have read through and tried the post below but it still does not get me there.
Split a collection into `n` parts with LINQ?

A:

You can use LINQ's GroupBy:
Dim tbl1 = New DataTable
tbl1.Columns.Add("ID", GetType(Int32))
tbl1.Columns.Add("Text", GetType(String))
For rowIndex As Int32 = 1 To 11
    tbl1.Rows.Add(rowIndex, "Row " & rowIndex)
Next

Dim tableCount = 10  ' what the user entered '
Dim divisor = tbl1.Rows.Count / tableCount ' needed to identify each group '
Dim tables = tbl1.AsEnumerable().
            Select(Function(r, i) New With {.Row = r, .Index = i}).
            GroupBy(Function(x) Math.Floor(x.Index / divisor)).
            Select(Function(g) g.Select(Function(x) x.Row).CopyToDataTable())

