title: Parallel For - Save In Database
body: <p>I'm having a lot of data to import into my database.</p>
  
  <p>These data need of treatment, filtration, joint, among other things. So everything is being done with objects, carrying all tables before starting, and saving all objects to the end.</p>
  
  <p>After all processing, I run the command to save. This is taking too long. I thought of practicing a different approach:</p>
  
  <blockquote>
  <pre><code>Task.Factory.StartNew(() =&gt;
          {
              while (allCSV.Length % 4 != 0)
              {
                  Save(allCSV.FirstOrDefault());
                  allCSV = allCSV.Skip(1).ToArray();
              }
  </code></pre>
  </blockquote>
  
  <pre><code>            int count1 = 0;
              int middle = allCSV.Length / 4;
              int count2 = middle;
              int count3 = middle * 2;
              int count4 = middle * 3;
              Parallel.For(0, 4, new Action&lt;int&gt;((i) =&gt;
              {
                  switch (i)
                  {
                      case 0:
                         for (int j = 0; j &lt; middle; j++)
                         {
                           Save(allCSV[count1]);
                           count1++;
                         }
                      break;
                      case 1:
                         for (int k = 0; k &lt; middle; k++)
                         {
                           Save(allCSV[count2]);
                           count2++;
                         }
                      break;
                      case 2:
                         for (int l = 0; l &lt; middle; l++)
                         {
                           Save(allCSV[count3]);
                           count3++;
                         }
                      break;
                      case 3:
                         for (int m = 0; m &lt; middle; m++)
                         {
                           Save(allCSV[count4]);
                           count4++;
                         }
                      break;
                   }
                }
            }
  </code></pre>
  
  <p>That would be correct? What would be the best way to streamline my process?</p>
  
  <p>Some considerations, the computer used is the latest generation. The database is MySQL.</p>
  
  <p>The ORM used to save, it is highly simple, does not have any kind of optimizations.</p>