I found a solution on the ASP.NET forums; I added
AsQueryable()
to the end of my
query
variable and it worked!
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