# I want to Split Datatable rows based on number

I have datatable and i want to split the number of rows based on foreach 12 row for example if the number of rows is 61 : that's mean 61 / 12 = 6 Tables 5 tables with 12 rows and the table number 6 have 3 rows C# Thanks alot

## Answers

You could use following LINQ query:

DataTable[] splittedtables = tbl.AsEnumerable() .Select((row, index) => new { row, index }) .GroupBy(x => x.index / 12) // integer division, the fractional part is truncated .Select(g => g.Select(x => x.row).CopyToDataTable()) .ToArray();

The array contains 6 tables, 5 with 12 rows, the last one has the remaining row.

Checked with this sample-data:

DataTable tbl = new DataTable(); tbl.Columns.Add("Column"); for(int i=0; i < 61; i++) tbl.Rows.Add(i.ToString());

You need to add using System.Linq;.