title: How To Break Up EntityReferenceCollection?
body: <p>I am writing a method that would break up the EntityReferenceCollection into a specified chunk size. I have the code for a list but no idea how to do so for an EntityReferenceCollection PLEASE HELP!</p>
  
  <pre class="lang-c# prettyprint-override"><code>public static List&lt;List&lt;T&gt;&gt; Split&lt;T&gt;(List&lt;T&gt; collection, int size)
          {
              var chunks = new List&lt;List&lt;T&gt;&gt;();
              var chunkCount = collection.Count() / size;
  
              if (collection.Count % size &gt; 0)
                  chunkCount++;
  
              for (var i = 0; i &lt; chunkCount; i++)
                  chunks.Add(collection.Skip(i * size).Take(size).ToList());
  
              return chunks;
          }
  </code></pre>
  
  <p>I am trying to do a bulk delete:</p>
  
  <pre class="lang-c# prettyprint-override"><code>string fetchXml = @" &lt;fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'&gt;
                          &lt;entity name='new_units'&gt;
                              &lt;link-entity name='new_alterunitorder' from ='new_orderlineid' to = 'new_unitsid' &gt;
                                  &lt;attribute name='new_alterunitorderid' /&gt;
                                  &lt;filter type='and'&gt;
                                      &lt;condition attribute='new_orderdate' operator='on-or-after' value='" + startDate.ToShortDateString() + @"' /&gt;
                                      &lt;condition attribute='new_orderdate' operator='on-or-before' value='" + endDate.ToShortDateString() + @"' /&gt;
                                      &lt;condition attribute='new_orderlineid' operator='eq' uiname='" + uiName + @"' uitype='new_units' value='" + unitOrderId + @"' /&gt;
                                  &lt;/filter&gt;
                              &lt;/link-entity&gt;
                          &lt;/entity&gt;
                      &lt;/fetch&gt;";
  
                      EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchXml));
  
                      var entRefCollection = new EntityReferenceCollection();
  
                      foreach (var id in result.Entities)
                      {
                          var reference = id.GetAttributeValue&lt;EntityReference&gt;("new_alterunitorderid");
                          entRefCollection.Add(reference);
                      }
  
                      if(entRefCollection.Count &lt; 1000)
                      {
                          Bulk.BulkDelete(service, entRefCollection);
                      }
                      else
                      {
                          Bulk.BulkDelete(service, entRefCollection_one);  //1st half of EntityReferenceCollection
                          Bulk.BulkDelete(service, entRefCollection_two);
  
                      }
  </code></pre>