I had read this answer ADO.NET DataTable/DataRow Thread Safety, and can\'t understand some things. Particularly I can\'t understand [2] article. What kind of wrapper I need
Introduction
If the concurrency or parallelism is a requirement for the DataTable object program, is possible to make this. Let's view two examples (basically, we will see the use of AsEnumerable() method as common in all examples):
1-Parallel iteraction on DataTable:
The .NET provide native resources to iterate in Parallel on DataTable, as the code bellow:
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("NAME");
dt.Rows.Add(1, "One");
dt.Rows.Add(2, "Two");
dt.Rows.Add(3, "Three");
dt.PrimaryKey = new DataColumn[] { dt1.Columns["ID"] };
Parallel.ForEach(dt.AsEnumerable(), row =>
{
int rowId = int.Parse(row["ID"]);
string rowName = row["NAME"].ToString();
//TO DO the routine that useful for each DataRow object...
});
2-Adding multiples items to DataTable:
I think this is the non-trivial approach because the core of DataTable is not a thread-safe collection/matrix; then, you need the support of ConcurrentBag to guarantees to not break an Exception on your code.
At the "ConcurrentBag - Add Multiple Items?", I write a detailed example with add items from DataTable objects to ConcurrentBag derived class, considering the programming needs to use concurrency on DataTables. Then, it's possible to convert the ConcurrentBag collection to DataTable after making the program business rule additions on ConcurrentBag enjoying the parallelism resources.