title: Linq results to IEnumerable invalid cast
body: <p>Question for Linq users out there: I'm getting an <code>InvalidCastexception: Specified cast is not valid</code> whenever I try to obtain an <code>IEnumerable</code> from <code>revs</code> after making the Linq query. There database is populated and it <em>should</em> be returning values.</p>
  
  <p>Specifically, the error is occurring on the line <code>List&lt;PDP&gt; rev = revs.ToList&lt;PDP&gt;();</code></p>
  
  <p>Any ideas what's going on?</p>
  
  <pre><code>short ret;
  using (DataContext db = new DataContext())
  {
      var play = from p in db.PDP
          where p.ID == id
          select p;
      var revs = play.OrderByDescending(p =&gt; p.revision);
      List&lt;PDP&gt; rev = revs.ToList();
      var revNum = revs.ToList().Count() &gt; 0 ? rev.First().revision : 0;
      ret = (short)revNum;
  }
  </code></pre>
  
  <p><strong>EDIT</strong><br>
  I've clarified some parts of the code.</p>
  
  <p><strong>EDIT 2</strong><br>
  <code>rev</code> exists as a debugging variable to narrow down where the error was.</p>
  
  <p>The original code was:</p>
  
  <pre><code>short ret;
  using (GasForecastDataContext db = new GasForecastDataContext())
  {
      var play = from p in db.PDP
          where p.Play_ID == play_id
          select p;
      var revs = play.OrderByDescending(p =&gt; p.revision);
      var revNum = revs.Count &gt; 0 ? rev.First().revision : 0;
      ret = (short)revNum;
  }
  </code></pre>