views:

63

answers:

2

Hi, sorry for my English. So, here is my question I'm trying to update DataTable by PLINQ Here is my code

DataTable table = new DataTable();

table.Columns.Add(new DataColumn("val", typeof(decimal)));

int N = 1000000;

for (int i = 0; i < N; i++) table.Rows.Add(new object[] { i });

table.AsEnumerable().AsParallel().ForAll(row => row["val"] = 3);

But there is exception:"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

Please, help me

+2  A: 

Well, I can tell you right now that modifying the rows of a DataTable in parallel is not Kosher (from the MSDN documentation on the DataTable class):

This type is safe for multithreaded read operations. You must synchronize any write operations.

So while I'm not sure exactly what's causing the particular exception you mention, I know that you really shouldn't be attempting this as it is unsupported.

Dan Tao
Yes, this looks like the right answer.
Henk Holterman
A: 

Found solution:

table.AsEnumerable().AsParallel().ForAll(row => { lock(table)row["val"] = 3; });

But after that - parallel makes no sense

feniks