Today we will discuss how we can divide or split a very large data table into fixed size of chunks?
Scenario:
Suppose there is a data table which has 1000 Rows.
When you perform for each loop on data table and read each row at that time it will take too much time.
If we devide 1000 Rows datatable into 10 fixed sizes (e.g. 100 Rows) datatable, It will take less time.
Let's check how to achieve it.
Here is a code.
Scenario:
Suppose there is a data table which has 1000 Rows.
When you perform for each loop on data table and read each row at that time it will take too much time.
If we devide 1000 Rows datatable into 10 fixed sizes (e.g. 100 Rows) datatable, It will take less time.
Let's check how to achieve it.
Here is a code.
private static List<DataTable> SplitTable(DataTable originalTable, int batchSize)
{
List<DataTable> tables = new List<DataTable>();
int i = 0;
int j = 1;
DataTable newDt = originalTable.Clone();
newDt.TableName = "Table_" + j;
newDt.Clear();
foreach (DataRow row in originalTable.Rows)
{
DataRow newRow = newDt.NewRow();
newRow.ItemArray = row.ItemArray;
newDt.Rows.Add(newRow);
i++;
if (i == batchSize)
{
tables.Add(newDt);
j++;
newDt = originalTable.Clone();
newDt.TableName = "Table_" + j;
newDt.Clear();
i = 0;
}
}
return tables;
}
For More Updates stay connected On: Learn2All Facebook Page
Hi Dhruvin, will it be faster than usual for loop?
ReplyDeleteNo , It will take same time.
DeleteBut it is useful when you are performing Datatable.select
in your code.
When we use datatable.select and find something from datatable at that time it will very useful.
Hi, useful means? can u mention any?
DeleteDatatable.select
Deletemeans suppose if you want to find employee whose name is "dhruvin" and salary is 5000.
At that time 100 row datatable speeds faster
Thank you, while replying if possible link is can provided?
DeleteThis comment has been removed by the author.
ReplyDelete