开发者

ASP.NET MVC: Cannot convert ObjectQuery -> Linq.IQueryable

i must be doing something wrong or maybe i don't get this. I'm trying to fill 'PropAllMenus' (that has a bunch of properties) with data coming from my entity framework. However, when doing the conversion trough a function i get a 'InvaldCastExeption' in my 'WeekMenuRepository'. Here is the code:

PropAllMenus

Public Class PropAllMenus

    Private _MenuID As Integer
    Public Property MenuID() As Integer
        Get
            Return _MenuID
        End Get
        Set(ByVal va开发者_Go百科lue As Integer)
            _MenuID = value
        End Set
    End Property

    Private _Name As String
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    Private _DaypartID As Integer
    Public Property DaypartID() As Integer
        Get
            Return _DaypartID
        End Get
        Set(ByVal value As Integer)
            _DaypartID = value
        End Set
    End Property

End Class

WeekMenuRepository

Private _db As New EDMWeekmenuEntities()
    Public Function ListAllMenus() As IQueryable(Of PropAllMenus) Implements IWeekMenuRepository.ListAllMenus
        Dim result = From p In _db.Menus _
                     Select p
        Return result

    End Function

HomeController

Dim DoThings As New WeekMenuRepository()
     Function Index() As ActionResult

            Return View(DoThings.ListAllMenus().ToList)

End Function


PropAllMenus isn't an entity type, so the EF can't implicitly convert an entity type like Menu to it.

In other words, your function result type is IQueryable(Of PropAllMenus), but your query is returning something like ObjectQuery(Of Menu). If you could change your query to return ObjectQuery(Of PropAllMenus) then the implicit cast (when you call Return) to IQueryable would work.

You need to do something like:

    Dim result = From p In _db.Menus _
                 Select New PropAllMenus With
                 {
                     .MenuId = p.MenuId,
                     .Name = p.Name,
                     // etc.
                 }

By the way, I noticed that you have asked 6 questions, but have never accepted any answers, nor uploaded anything. You should press the "up arrow" next to any answers which you find helpful, and press the "checkmark" icon next to the single most helpful answer for a question you've asked. It's a way of saying "thank you."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜