title: C# split array in groups of 30 then call api
body: <p>I would like to ask for help with a loop.</p>
  <p>My method receives a JSON string with names and email addresses, it uses this string of objects to pass to an external API POST request.</p>
  <p>I just found out that the API can process only 30 items at a time otherwise it does not validate.</p>
  <p>I would like to learn how to check if the json string contains more than 30 items, if it does, then process 30 items at a time when calling the api. Is it possible to get a sample of how this loop would need to be?</p>
  <p>My method currently does these steps:</p>
  <pre><code>public ActionResult CreatePanelists(Parameters data)
  {
      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  
      var webId = data.webId;
      var jwt_token = JWTToken.GetJWTToken();
      var panelists = JsonConvert.SerializeObject(data.panelists);   &lt;- this is the array
      var client = new RestClient($&quot;https://api/{webId}/panelists&quot;);
      var request = new RestRequest(Method.POST);
  
      request.AddHeader(&quot;content-type&quot;, &quot;application/json&quot;);
      request.AddHeader(&quot;authorization&quot;, $&quot;Bearer {jwt_token}&quot;);
      request.AddParameter(&quot;application/json&quot;, $&quot;{{\&quot;panelists\&quot;: {panelists}}}&quot;, ParameterType.RequestBody);
      IRestResponse response = client.Execute(request);
  
      return new JsonResult { Data = new { status = response.StatusCode, message = response.Content} };
  
  }
  </code></pre>
  <p>Any help would be awesome.</p>
  <p>Thank you very much.</p>
  <p><strong>UPDATE</strong></p>
  <p>I added the example code, and seeing a few errors:</p>
  <pre><code>'index': a parameter or local variable cannot have the same name as a method type parameter
  
  Syntax error, '=&gt;' expected
  
  Invalid expression term ')'
  
  'item': a parameter or local variable cannot have the same name as a method type parameter
  </code></pre>
  <p><strong>UPDATE2</strong></p>
  <p>The returns part is complaining now, response is within the loop and not accessible to the return</p>
  <pre><code>The name response does not exists in the current context.
  
  return new JsonResult { Data = new { status = response.StatusCode, message = response.Content} };
  </code></pre>
  <p><strong>UPDATE3</strong></p>
  <pre><code>public ActionResult CreatePanelists(Parameters data)
  {
      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  
      var results = new List&lt;string&gt;();
      var webId = data.webId;
      var jwt_token = JWTToken.GetJWTToken();
      
      var batches = data.panelists
          .Select((item, index) =&gt; new { item, index })
          .GroupBy(t =&gt; t.index / 30)
          .Select(g =&gt; g.Select(t =&gt; t.item))
          .Select(batch =&gt; JsonConvert.SerializeObject(batch));
  
      foreach (var batch in batches)
      {
          var client = new RestClient($&quot;https://api/meetings/{webId}/panelists&quot;);
          var request = new RestRequest(Method.POST);
  
          request.AddHeader(&quot;content-type&quot;, &quot;application/json&quot;);
          request.AddHeader(&quot;authorization&quot;, $&quot;Bearer {jwt_token}&quot;);
          request.AddParameter(&quot;application/json&quot;, $&quot;{{\&quot;panelists\&quot;: {batch}}}&quot;, ParameterType.RequestBody);
          IRestResponse response = client.Execute(request);
          string JSON = JsonConvert.SerializeObject(response);
          
          results.AddItem(JSON);
      }
  
      return new JsonResult { Data = results };
  </code></pre>
  <p>AddItem:</p>
  <pre><code>    public static void AddItem&lt;T&gt;(this List&lt;T&gt; list, T item)
      {
          if (!list.Contains(item))
          {
              list.Add(item);
          }
      }
  </code></pre>
  <p>If you have any suggestions as to how to make the code better, please let me know</p>