I have a process that uses DataTables to hold rows of data and at the end of the process it uses SQLBulkCopy to write the resulting data to the database. If I wanted to use Parallel.ForEach around the loop of Customers being processed I will have to use Locks on the DataTable / DataRow's as its not thread safe. All I really need is a container to hold the results and then the ability to write this data to SQL at the end. Would it be advisable to use a ConcurrentBag collection to hold the data as its threadsafe and then at the end of the process (outside of the Parallel.ForEach) convert the ConcurrentBag of objects containing the data into a DataTable and then use SQLBulkCopy to write the data?
ConcurrentBag<Data1> Data1 = new ConcurrentBag<Data1>();
ConcurrentBag<Data2> Data2 = new ConcurrentBag<Data2>();
// Do stuff for Data1
// Do stuff for Data2
foreach (obj in Data1)
// Convert to DataRow and add to DataTable
SQLBulkCopy DataTable to SQL.