title: System.InvalidCastException: Specified cast is not valid. Error
body: <p>I am on a C# ASP.NET project. </p>
  
  <p>I have a MySQL table with a userid field of type <code>int</code>.</p>
  
  <p>Now I want to get the number of rows where value of userid equals certain value using LINQ.</p>
  
  <p>To achieve this, I wrote the following method: </p>
  
  <pre><code>public int getCount(int usercode) {
      int count = 0;
      DataTable mytable = getAllRowsAndReturnAsDataTable(); // assigning a DataTable value to mytable. 
      if (mytable.Rows.Count &gt; 0) { 
          count = (from x in mytable.AsEnumerable() where x.Field&lt;Int32&gt;("userid") == usercode select x).Count();
      } 
      return count;
  }
  </code></pre>
  
  <p>but it is showing error <code>System.InvalidCastException: Specified cast is not valid.</code> showing <code>count = (from x in mytable.AsEnumerable() where x.Field&lt;Int32&gt;("userid") == usercode select x).Count();</code> in red highlight area.</p>
  
  <p>I don't know what I did wrong here. Please help.</p>