How to split or split a DataTable in C #?

I want to split a DataTable so that I can load my chunks from one place to another.

for instance

select the first 100 rows.
select the next 100 lines.
select the next 100 lines, etc.

Is there a way to do this, like a cursor in a database? I do not like to use loops etc. To count the lines.

+6
source share
5 answers

YourDataTable.Select () gives you an array of Dat

What about linq?

The Fro example YourDataTable.Select (x => x).Take (100).ToEnumerable() gives you the first 100 DataRows and YourDataTable.Select (x => x).Skip(100).Take (100).ToEnumerable() for next 100.

+9
source

Try the following:

 public static class DataExtensions { public static IEnumerable<IEnumerable<DataRow>> Partition(this DataTable dataTable, int partitionSize) { var numRows = Math.Ceiling((double)dataTable.Rows.Count); for(var i = 0; i < numRows / partitionSize; i++) { yield return Partition(dataTable, i * partitionSize, i * partitionSize + partitionSize); } } private static IEnumerable<DataRow> Partition(DataTable dataTable, int index, int endIndex) { for(var i = index; i < endIndex && i < dataTable.Rows.Count; i++) { yield return dataTable.Rows[i]; } } } var partitions = dataTable.Partition(100); 

Doing:

 dataTable.Skip(0).Take(100); dataTable.Skip(100).Take(100); dataTable.Skip(200).Take(100); dataTable.Skip(300).Take(100); 

Iterates 0 times and takes 100 on first run. Then iterate through 100 lines, take 100, then repeat 200 lines, then take 100, etc.

The above will do lazy fetch and only hit every line once

+6
source

This is an easy way to do this:

 public DataSet test(DataSet ds, int max) { int i = 0; int j = 1; DataSet newDs = new DataSet(); DataTable newDt = ds.Tables[0].Clone(); newDt.TableName = "Table_" + j; newDt.Clear(); foreach (DataRow row in ds.Tables[0].Rows) { DataRow newRow = newDt.NewRow(); newRow.ItemArray = row.ItemArray; newDt.Rows.Add(newRow); i++; if (i == max) { newDs.Tables.Add(newDt); j++; newDt = ds.Tables[0].Clone(); newDt.TableName = "Table_" + j; newDt.Clear(); i = 0; } } return newDs; } 

Can you try

+3
source

Use linq select part of the record this link in stack overflow can be useful Split the collection into n parts using LINQ?

+2
source

Check: splitting large amounts of data into smaller batches from c-sharpcorner.com

 internal static List<datatable> SplitTable(DataTable originalTable, int batchSize) { List<datatable> tables = new List<datatable>(); DataTable new_table = new DataTable(); new_table = originalTable.Clone(); int j = 0; int k = 0; if (originalTable.Rows.Count &lt;= batchSize) { new_table.TableName = "Table_" + k; new_table = originalTable.Copy(); tables.Add(new_table.Copy()); } else { for (int i = 0; i &lt; originalTable.Rows.Count; i++) { new_table.NewRow(); new_table.ImportRow(originalTable.Rows[i]); if ((i + 1) == originalTable.Rows.Count) { new_table.TableName = "Table_" + k; tables.Add(new_table.Copy()); new_table.Rows.Clear(); k++; } else if (++j == batchSize) { new_table.TableName = "Table_" + k; tables.Add(new_table.Copy()); new_table.Rows.Clear(); k++; j = 0; } } } return tables; } 
+1
source

Source: https://habr.com/ru/post/901485/


All Articles