question

TZacks-2728 avatar image
0 Votes"
TZacks-2728 asked TZacks-2728 edited

How to get row index when used AsParallel().ForAll()

See here i am using AsParallel().ForAll(). x contains datarow but how to get row or index? i tried this syntax which does not work
ForAll((x,r) => please advise how to get row index when used AsParallel().ForAll(). Thanks

             dt.AsEnumerable().AsParallel().ForAll(x =>
             {
                 data.Add(x.Field<string>("ID") + " " + Thread.CurrentThread.ManagedThreadId);
                 a = x.Field<string>("ID");
                 y++;
             });


I am following this approach given by Mr Viorel-1

Thanks you so much for code sample. i followed your guide line.

                 DataTable dt = new DataTable();
                 dt.Columns.Add("ID", typeof(int));
                 dt.Columns.Add("Name", typeof(string));
                 dt.Columns.Add("Salary", typeof(double));
                 DataRow dr = null;
        
                 dr = dt.NewRow();
                 dr["ID"] = 1;
                 dr["Name"] = "Manju";
                 dr["Salary"] = 1500;
                 dt.Rows.Add(dr);
        
                 dr = dt.NewRow();
                 dr["ID"] = 2;
                 dr["Name"] = "Dibyendu";
                 dr["Salary"] = 2000;
                 dt.Rows.Add(dr);
        
                 dr = dt.NewRow();
                 dr["ID"] = 3;
                 dr["Name"] = "Suraj";
                 dr["Salary"] = 5000;
                 dt.Rows.Add(dr);
        
                 dr = dt.NewRow();
                 dr["ID"] = 4;
                 dr["Name"] = "Mukti";
                 dr["Salary"] = 1200;
                 dt.Rows.Add(dr);
        
                 dr = dt.NewRow();
                 dr["ID"] = 5;
                 dr["Name"] = "Suvo";
                 dr["Salary"] = 1000;
                 dt.Rows.Add(dr);
        
                 List<Employee> emp = new List<Employee>();
                 dt.AsEnumerable().Select((r, idx) => new { r, idx }).AsParallel().ForAll(x =>
                 {
                     emp.Add(new Employee
                     {
                         RowIndex = x.idx+1,
                         ThreadID = Thread.CurrentThread.ManagedThreadId,
                         ID = x.r.Field<int>("ID"),
                         Name = x.r.Field<string>("Name"),
                         Salary = x.r.Field<double>("Salary")
                     });
                 });                




dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
1 Vote"
Viorel-1 answered TZacks-2728 commented

Try something like this:

 dt.AsEnumerable( ).Select( ( r, y ) => new { r, y } ).AsParallel( ).ForAll( p => { . . . } );

Inside the ". . ." body, p.r is the DataRow, p.y — row index.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks you so much for code sample. i followed your guide line.

             dt.AsEnumerable().Select((r,idx) => new { r, idx } ).AsParallel().ForAll(x =>
             {
                 data.Add(x.idx.ToString() + " " + Thread.CurrentThread.ManagedThreadId);
                 //data.Add(x.Field<string>("ID") + " " + Thread.CurrentThread.ManagedThreadId);
                 a = x.r.Field<string>("ID");
                 //x.r.fi
                 //y++;
             });
0 Votes 0 ·
TimonYang-MSFT avatar image
1 Vote"
TimonYang-MSFT answered

Is it okay to use tuples to get results?

             List<(string,int)> data = new List<(string, int)>();
             dataTable.AsEnumerable().AsParallel().ForAll(x =>
             {
                 data.Add((x.Field<int>("ID") + " " + Thread.CurrentThread.ManagedThreadId, dataTable.Rows.IndexOf(x)));
             });

Did I understand what you mean correctly?


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

vb2ae avatar image
0 Votes"
vb2ae answered vb2ae edited

AsParallel will allow more than row to be processed at one time so y++ will not give you the row index. You would need to use one of the columns to figure out the row index

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.