title: Collection was modified; enumeration operation might not execute in LinQ
body: <p>I get data from a site and insert to DataTable <code>dt</code>.</p>
  
  <pre><code>if (checked = true)
  {
      while (true)
      {
          breakIt = true;
          try
          {
              string html = browser_array[idx].FindElementByXPath("id('tabbox')").GetAttribute("outerHTML");
              HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
              doc.LoadHtml(html);
              HtmlNodeCollection nodesMatchingXPathlop = doc.DocumentNode.SelectNodes("//*[contains(@class,'tabbox_F')]/table/tbody/tr");
              if (nodesMatchingXPathlop.Count == 0)
              {
                  break;
              }
              else
              {
                  for (int j = 0; j &lt; nodesMatchingXPathlop.Count; j++)
                  {
                      try
                      {
                          listHeader[1].Clear();
                          HtmlNodeCollection listTD3 = nodesMatchingXPathlop[j].SelectNodes("td");
                          if (nodesMatchingXPathlop[j].SelectNodes("td").ToString() == "")
                              return;
                          if (nodesMatchingXPathlop[j].SelectNodes("td").Count == 10)
                          {
                              if (listHeader[1].Count == 0)
                                  continue;
                              else
                                  Insert(dt, listHeader[1]);
                          }
                          else
                          {
                              continue;
                          }
                      }
                      catch (Exception ex) { throw; }
                  }
              }
              if (breakIt)
              {
                  break;
              }
          }
          catch (Exception ex) { throw; }
      }
  }
  </code></pre>
  
  <p>So, this function is <code>Insert(DataTable dt, List&lt;String&gt; listHeader)</code>:</p>
  
  <pre><code>static void Insert(DataTable dt, List&lt;String&gt; listHeader)
  {
      DataRow row = dt.NewRow();
      string[] array = listHeader.ToArray();
      string newArray = "";
      newArray = Regex.Replace(array[1], @"\s+", " ");
      string arrayHindiCap2 = "";
      arrayHindiCap2 = Regex.Replace(array[2], @"\s+", " ").Replace("&amp;nbsp;", "");
      string arrayHindiCap5 = "";
      arrayHindiCap5 = Regex.Replace(array[5], @"\s+", " ").Replace("&amp;nbsp;", "");
  
      try
      {
          if (!string.IsNullOrWhiteSpace(arrayHindiCap2))
          {
              InsertCondition(arrayHindiCap2, arrayHindiCap5, newArray, listHeader, array);
          }
          else if (!string.IsNullOrWhiteSpace(arrayHindiCap5))
          {
              InsertCondition(arrayHindiCap2, arrayHindiCap5, newArray, listHeader, array);
          }
      }
      catch (Exception ex) { throw; }
  }
  </code></pre>
  
  <p>I have a table like this:</p>
  
  <pre><code>Option         B0_1           Col 1            Col 2
  
  op1            10             89
  </code></pre>
  
  <p>I will get an index of rows in DataTable when it correct with a condition.</p>
  
  <pre><code>static void InsertCondition(string arrayHindiCap2, string arrayHindiCap5, string newArray, List&lt;String&gt; listHeader, string[] array)
  {
      try 
      {
          index = dt.AsEnumerable()
               .Select((rowF, idx) =&gt; new { rowF, idx })
               .Where(item =&gt;
                GetFirstB(numB) == (item.rowF.Field&lt;string&gt;("B0_1") != null ? item.rowF.Field&lt;string&gt;("B0_1") : "") &amp;&amp;
                GetFirstWord(optionString) == (item.rowF.Field&lt;string&gt;("Option") != null ? item.rowF.Field&lt;string&gt;("Option") : ""))
               .Select(item =&gt; item.idx)
               .ToArray();
          }
          catch(Exception ex) { throw; }
  }
  </code></pre>
  
  <p>I define <code>GetFirstB()</code> and <code>GetFirstWord()</code>:</p>
  
  <pre><code>private static string GetFirstWord(string str)
  {
      try
      {
          return string.Concat(str.Split(' ').Take(2));
      }
      catch (Exception ex) { throw; }
  }
  
  private static string GetFirstB(string str)
  {
      try
      {
          if (str == null)
              return null;
          if (str.Equals("&amp;nbsp;"))
              return null;
          string[] strNew;
          strNew = str.Split(' ');
          strNew = strNew.Where(c =&gt; c != "").ToArray();
          return string.Concat(strNew.FirstOrDefault());
      }
      catch (Exception ex) { throw; }
  }
  </code></pre>
  
  <p>Following @ScottChamberlain and @PhilipKendall suggest.
  I edit my code and tried debug again.</p>
  
  <p>After Copy exception copies detail to clipboard it shows like this:</p>
  
  <pre><code>System.InvalidOperationException was caught
    HResult=-2146233079
    Message=Collection was modified; enumeration operation might not execute.
    Source=System.Data
    StackTrace:
         at System.Data.RBTree`1.RBTreeEnumerator.MoveNext()
         at System.Linq.Enumerable.&lt;CastIterator&gt;d__b1`1.MoveNext()
         at System.Linq.Enumerable.&lt;SelectIterator&gt;d__7`2.MoveNext()
         at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
         at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
         at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
         at LAPSOFT.Program.InsertCondition(String arrayHindiCap2, String arrayHindiCap5, String newArray, List`1 listHeader, String[] array) in d:\lapsoft-project\CA CUOC - Copy\Program.cs:line 418
         at LAPSOFT.Program.Insert(DataTable dt, List`1 listHeader) in d:\project\CheckCopy\Program.cs:line 548
    InnerException: 
  </code></pre>