Split Datatable into 4 different tables

@thomasb82
lets assume following variables:

NoOfSegments is indicating how many splits we do want have

Doing an integer division to find out how many rows will be in a split (we cannot fetch a rows count with fractions e.g. 14.25 rows
grafik

with following LINQ we can produce a list of datatables with the splits:
grafik

(From i In Enumerable.Range(0,NoOfSegments)
Let opart = dtData.AsEnumerable.Skip(i*FullSegmentSize).toList
Let spart = If(i = (NoOfSegments - 1), opart, opart.Take(FullSegmentSize)).toList
Select spart.CopyToDataTable).toList

line1 - creating a sequence with the segment numbers (0,1,2,3)
line2 - skip rows acordingly the looped segment
line3 - take rows of an amount of FullSegmentSize or take all remaining rows when it is the last segment

line4 - Copy the segment rows to a DataTable, add it to returned tablelist

find starter help here:
DT_Slice_ByNSegments.xaml (7.1 KB)

7 Likes