Is parallel.foreach is best way to improve speed of executing this code
<p>I have code that i need to rewrite to improve speed of executing original code :</p>

<p>Data class :</p>

<pre><code>public class Data
{
    public string Id {get;set;}
    ... Other properties 
}
</code></pre>

<p>Services : ( There are more than 2 i jus give u 2 for example ) </p>

<pre><code>public class SomeService1
{
    public Result Process(Data data)
    {
        //Load data from different services hire 
    }
}

public class SomeService2
{
    public Result Process(Data data)
    {
        //Load data from different services hire 
    }
}
</code></pre>

<p>Actual method </p>

<pre><code>public void Calculate (List&lt;Data&gt; datas)
{
    Result result;
    SomeService1 someService1 = new SomeService1();
    SomeService2 someService2 = new SomeService2();
    // In this place list of data have about 2000 elements 
    foreach(var data in datas)
    {
        switch(data.Id)
        {
            case 1:
                result = someService1.Process(data)
                break;
            case 2:
                result = someService2.Process(data)
                break;
            default:
                result = null;
        }
    ProcesAndSaveDataToDatabase(result);
    }
}
</code></pre>

<p>The method <code>Calculate</code> i taking List as parameter for every element in this list it grabing data from outside service ( service is determine by id in Data class ). Then it process this data and saving to database. For 2000 elements whole operation is taking about 8 min. 70 % of the time is gathering data from outside service. I must change that time. I have only one idea to do this but to be honest i can't test it with data because only data are on Production environment ( and testing on production is bad idea ). I have one idea. Can you look at it and advice to me if i going in right direction ? </p>

<p>Data class :</p>

<pre><code>public class Data
{
    public string Id {get;set;}
    ... Other properties 
}
</code></pre>

<p>Services : ( There are more than 2 i jus give u 2 for example ) </p>

<pre><code>public class SomeService1 : IService
{
    public Result Process(Data data)
    {
        //Load data from different services hire 
    }
}

public class SomeService2 : IService
{
    public Result Process(Data data)
    {
        //Load data from different services hire 
    }
}
</code></pre>

<p>IService : </p>

<pre><code>public interface IService
{
    Result Process(Data data);
}
</code></pre>

<p>Actual method : </p>

<pre><code>Public void Calculate (List&lt;Data&gt; datas)
{
    var split= from data in datas group data by data.Id into newDatas select newDatas
    // Different list split by Id
    Parallel.Foreach(split, new ParallelOptions{MaxDegreeOfParallelism = 4}, datas =&gt; 
    {
        Result result;
        IService service = GetService(datas.FirsOfDefault().Id);
        if(service == null) return;
        foreach(var data in datas)
        {
            result = service.Process(data)
            ProcesAndSaveDataToDatabase(result);
        }
    }); 
}

private IService GetService(string id)
{
      IService service = null;
      if(id == null ) return service;
      switch(id)
      {
           case 1:
                service = new SomeService1();
                break;
           case 2: 
                service = new SomeService2();
                break;
      }
      return service;
 }
</code></pre>

<p>In this idea i try to split the different services data to different threads. So in in list we will have 20 items with <code>Id  = 1</code> and 10 items with <code>Id = 2</code> it should create 2 separated thread and process it discretely this should allow me to cut off the execute time. Is this is good way ? Is any other possibilities to improve this code ? </p>

<p>Thanks  </p>