I'm trying to get data for a gridview and have a cast error.
In the code below, if I just return "myuserlist" - all is good. As soon as I try to select only 2 fields (as in this code), I get the casting error. I've looked at other comments posted here regarding this problem and tried a number of things with no success.
Imports System.Linq
Imports RoutesEntities
Partial Class ProfileTest
Inherits System.Web.UI.Page
Private myentity As New RoutesEntities()
Public Function GridView1_GetData() As IQueryable(Of AllUser)
Return From myuserlist In myentity.AllUsers Select New With {myuserlist.UserName,myuserlist.Email}
End Function
Error:
System.InvalidCastException was unhandled by user code
HResult=-2147467262
Message=Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[VB$AnonymousType_0`2[System.String,System.String]]' to type 'System.Linq.IQueryable`1[AllUser]'.
Source=App_Web_oelfrca5
StackTrace:
at ProfileTest.GridView1_GetData() in C:\xxx\yyy\zzz\Profile.aspx.vb:line 10
InnerException:
I tried this (among other things):
Return From myuserlist In myentity.AllUsers Select New AllUser() With {
.UserName = myuserlist.UserName,
.Email = myuserlist.Email}
and got this:
Exception Details: System.NotSupportedException: The entity or complex type 'RoutesModel.AllUser' cannot be constructed in a LINQ to Entities query.