title: C# Datatable .AsParallel() AsOrdered() not giving data in order
body: <p>I have created data table and store some test data there just to see how <strong>AsParallel() with AsOrdered()</strong> works but when i test the code then i saw i am not getting data in any specific order. ever time i am executing code the order is getting different. so what is lack in my code for which i am not getting data in order?</p>
  <h2>see my sample code</h2>
  <pre><code>DataTable dtTest = new DataTable();
  dtTest.Columns.Add(&quot;ID&quot;,typeof(int));
  dtTest.Columns.Add(&quot;Name&quot;, typeof(string));
  dtTest.Columns.Add(&quot;Salary&quot;, typeof(int));
  
  DataRow dr = dtTest.NewRow();
  dr[&quot;ID&quot;] = 1;
  dr[&quot;Name&quot;] = &quot;Rom&quot;;
  dr[&quot;Salary&quot;] = &quot;2000&quot;;
  dtTest.Rows.Add(dr);
  
  dr = dtTest.NewRow();
  dr[&quot;ID&quot;] = 2;
  dr[&quot;Name&quot;] = &quot;David&quot;;
  dr[&quot;Salary&quot;] = &quot;5000&quot;;
  dtTest.Rows.Add(dr);
  
  dr = dtTest.NewRow();
  dr[&quot;ID&quot;] = 3;
  dr[&quot;Name&quot;] = &quot;Samy&quot;;
  dr[&quot;Salary&quot;] = &quot;1200&quot;;
  dtTest.Rows.Add(dr);
  
  
  StringBuilder sb = new StringBuilder();
  sb.Append(&quot;&lt;table&gt;&quot;);
  sb.Append(&quot;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;ID&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Salary&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&quot;);
  sb.Append(&quot;&lt;tbody&gt;&quot;);
  
  var htmltables = dtTest.AsEnumerable().AsParallel().AsOrdered().Select(a =&gt;
  {
      MessageBox.Show(&quot;ID &quot; + a.Field&lt;int&gt;(&quot;ID&quot;).ToString());
  
       return   sb.Append(&quot;&lt;tr&gt;&lt;td&gt;&quot; + a.Field&lt;int&gt;(&quot;ID&quot;).ToString() 
                  + &quot;&lt;/td&gt;&lt;td&gt;&quot; + a.Field&lt;string&gt;(&quot;Name&quot;).ToString() 
                  + &quot;&lt;/td&gt;&lt;td&gt;&quot; + a.Field&lt;int&gt;(&quot;Salary&quot;).ToString()
                  +&quot;&lt;/td&gt;&lt;/tr&gt;&quot;);
      
  }).ToList();
  
  sb.Append(&quot;&lt;/tbody&gt;&lt;/table&gt;&quot;);
  </code></pre>
  <p>if anyone run my test code in .net 4.5.2 then must notice this ID order is getting different when i am showing through message box. i am talking about this line <code>MessageBox.Show(&quot;ID &quot; + a.Field&lt;int&gt;(&quot;ID&quot;).ToString());</code></p>
  <p>so when i am composing table inside parallel loop then records whose ID is 1 that is sometime coming at end or in the second position. so how can i use <strong>AsParallel() with AsOrdered()</strong> as result ID should be in order ?</p>
  <p>i do not want to use <strong>Lock{}</strong> statement because then freedom of multi thread will be finish.</p>
  <p>please guide me how to get data in order with multi threading. thanks</p>