Click here to Skip to main content
15,887,397 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I'm using some free source code to create an e-commerce site in WebForms. It is the Visual Basic version of Getting Started with ASP.NET 4.5 Web Forms and Visual Studio 2013. 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:

Unable to cast object of type 'WhereEnumerableIterator`1[LethalLibrary.Book]' to type 'System.Linq.IQueryable`1[LethalLibrary.Book]'.

Description: 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. 

Exception Details: System.InvalidCastException: Unable to cast object of type 'WhereEnumerableIterator`1[LethalLibrary.Book]' to type 'System.Linq.IQueryable`1[LethalLibrary.Book]'.

Does Linq from VS2017 differ from VS2013? How do I fix this?


What I have tried:

VB
Public Function GetBook(<QueryString("BookID")> 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 > 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


I've also added
VB
.Select(Function(p) p)
to the end of my .Where statement, but that hasn't worked, either.

VB
<pre>Public Function GetBook(<QueryString("BookID")> 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 > 0 Then
			query = query.Where(CType(Function(p) p.BookID = BookId, Func(Of Book, Boolean))).Select(Function(p) p)
		Else
			query = Nothing
		End If
		Return query
	End Function
Posted
Updated 7-May-18 19:07pm

Try use Enumerable.Cast(TResult) Method (IEnumerable) (System.Linq)[^] :
VB.NET
Dim query As IQueryable(Of Book) = db.Books.Cast(Of Book)() _
    .Where(Function(x) x.BookID = BookId) _
    .ToList()

or Enumerable.OfType(TResult) Method (IEnumerable) (System.Linq)[^]:
VB.NET
Dim query As IQueryable(Of Book) = db.Books.OfType(Of Book)() _
    .Where(Function(x) x.BookID = BookId) _
    .ToList()


For further details, please see: Supported and Unsupported LINQ Methods (LINQ to Entities) | Microsoft Docs[^]
 
Share this answer
 
I found a solution on the ASP.NET forums; I added AsQueryable() to the end of my query variable and it worked!

VB
Public Function GetBook(BookId As Nullable(Of Integer)) As IQueryable(Of Book)
    Dim BookTitle As String = "aaaa"
    Dim db = New ApplicationDbContext()
    Dim query As IQueryable(Of Book) = db.Books
    If BookId.HasValue AndAlso BookId > 0 Then
        query = (query.Where(CType(Function(p) p.BookId = BookId, Func(Of Book, Boolean)))).AsQueryable()
    ElseIf Not String.IsNullOrEmpty(BookTitle) Then
        query = (query.Where(Function(p) String.Compare(p.BookTitle, BookTitle) = 0)).AsQueryable()
    Else
        query = Nothing
    End If
    Return query
End Function
 
Share this answer
 
Comments
Maciej Los 8-May-18 3:58am    
5!You should accept your answer as a solution (green button) - formally to remove yor question from unanswered list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900