title: Unable to cast object of type 'WhereEnumerableIterator`1[Object]' to type 'System.Linq.IQueryable`1[Object]'
body: <p>I'm using some free source code to create an e-commerce site in WebForms. It is the Visual Basic version of <a href="https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/display_data_items_and_details" rel="nofollow noreferrer">Getting Started with ASP.NET 4.5 Web Forms and Visual Studio 2013</a>. I have been successful until I coded the ProductDetails.aspx.vb page. I replaced my object names and properties accordingly, but when I run the project, I get this error:</p>
  
  <p><em>Unable to cast object of type 'WhereEnumerableIterator<code>1[LethalLibrary.Book]' to type 'System.Linq.IQueryable</code>1[LethalLibrary.Book]'.</em></p>
  
  <p><strong>Description:</strong> An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. </p>
  
  <p><strong>Exception Details:</strong> System.InvalidCastException: Unable to cast object of type 'WhereEnumerableIterator<code>1[LethalLibrary.Book]' to type 'System.Linq.IQueryable</code>1[LethalLibrary.Book]'.</p>
  
  <p>Does Linq from VS2017 differ from VS2013? How do I fix this?</p>
  
  <pre><code>Public Function GetBook(&lt;QueryString("BookID")&gt; BookId As Nullable(Of Integer)) As IQueryable(Of Book)
      Dim db = New BookContext()
      Dim query As IQueryable(Of Book) = db.Books
      If BookId.HasValue AndAlso BookId &gt; 0 Then
          query = query.Where(CType(Function(p) p.BookID = BookId, Func(Of Book, Boolean)))
      ElseIf Not String.IsNullOrEmpty(BookTitle) Then
          query = query.Where(Function(p) String.Compare(p.BookTitle, BookTitle) = 0)
      Else
          query = Nothing
      End If
      Return query
  End Function
  </code></pre>