title: Why is LINQ query to DataTable not working?
body: <p>I have a DataTable, dt, that contains the following: <img src="https://i.stack.imgur.com/vDwRZ.jpg" alt="enter image description here"></p>
  
  <p>As you can see, there are two sets of files here: baseline (columns 1-3) and target (columns 4-6). So in this case I have 4 baseline files, m4, m5, m1, and m3 (m1 in row 5 is copied from row 3) and one target file, m1. The problem is that the information for the baseline file m1.txt is duplicated, so I'm trying to remove it using the following LINQ statement: </p>
  
  <pre><code>var extraneousRows = dt.Rows.Cast&lt;DataRow&gt;().
      Where(
          row =&gt; row["BASELINE_FOLDER"] == baselineSubfolder
          &amp;&amp; row["BASELINE_FILE"] == baselineFilename
          &amp;&amp; row["BASELINE_CHECKSUM"] == baselineChecksum
          &amp;&amp; row["STATUS"] == "remove"
      ).ToArray();
  
  foreach (DataRow dr in extraneousRows)
      {
          dt.Rows.Remove(dr);
      }
  </code></pre>
  
  <p>This should remove the 3rd row from the DataTable but it doesn't. The code works fine if I omit the      <code>&amp;&amp; row["STATUS"] == "remove"</code> line, so I know it's functional. And the values for baselineSubfolder, baselineFilename, and baselineChecksum are correct, so that's not the problem. </p>
  
  <p>But for some reason, when I include that line about the status column, it doesn't detect that that row is in the DataTable, even though it clearly is according to the DataSet Visualizer (photo above). So it's never entering the foreach loop and not removing the necessary files. Why???</p>
  
  <p>I maybe should mention that the baseline file information (first 4 rows) are being retrieved from a database, whereas the target file fields are being generated according to user input. I don't see how it would matter where the information is coming from, though, since I'm querying the DataTable directly...   </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Ok, after following the suggestions of idipous and Jamie Keeling, I've determined that the problem had to do with the foreach loop, which was never being populated. Since this query should only ever return a single row, I eliminated the loop altogether. My revised code looks like this: </p>
  
  <pre><code>var extraneousRows = dt.Rows.Cast&lt;DataRow&gt;().
      Where(
          row =&gt; row["BASELINE_FOLDER"] == baselineSubfolder
          &amp;&amp; row["BASELINE_FILE"] == baselineFilename
          &amp;&amp; row["BASELINE_CHECKSUM"] == baselineChecksum
          &amp;&amp; row["STATUS"] == "remove"
      ).SingleOrDefault();
  
  dt.Rows.Remove(extraneousRows);
  </code></pre>
  
  <p>For whatever reason, extraneousRows remains null and that last line is generating a runtime error: <code>IndexOutOfRangeException: The given DataRow is not in the current DataRowCollection</code></p>
  
  <p>Why isn't this working? </p>