0

我有以下嵌套视图模型:

Public Class ContentViewModel
    Public Property CONTENTID As Integer
    Public Property IMAGETOCONTENT() As IEnumerable(Of ImageToContentViewModel)
End Class

Public Class ImageToContentViewModel
    Public Property IMAGETOCONTENTID As Integer
    Public Property IMAGE As ImageViewModel
End Class

Public Class ImageViewModel
    Public Property IMAGEID As Integer
    Public Property TITLE As String
End Class

我正在尝试查询嵌套的视图模型:

Function Test() As ContentViewModel
    Dim dataObject = (From a In db.CMS_CONTENT.OfType(Of CMS_EVENT)()
                 Where a.CONTENTID = contentId AndAlso a.DELETED = 0
                 Select New ContentViewModel With {
                     .CONTENTID = a.CONTENTID,                        
                     .IMAGETOCONTENT = (From b In db.CMS_IMAGES_TO_CONTENT
                      Where b.CONTENTID = a.CONTENTID
                      Select New ImageToContentViewModel With {
                          .IMAGETOCONTENTID = b.IMAGETOCONTENTID,
                          .IMAGE = (From c In db.CMS_IMAGES
                                    Where c.IMAGEID = b.IMAGEID And c.DELETED = False
                                    Select New ImageViewModel With {
                                        .IMAGEID = c.IMAGEID,
                                        .TITLE = c.TITLE
                                    })
                      })
                 })

    Return dataObject 
End Function

我的问题是我无法返回填充的视图模型,因为我总是得到错误:

“System.Data.Objects.ObjectQuery`1[MvcApp.ContentViewModel]”类型的对象无法转换为“MvcApp.ContentViewModel”

4

1 回答 1

0

您的函数体创建一个IQueryableof ContentViewModel(实现为System.Data.Objects.ObjectQuery`1[MvcApp.ContentViewModel]),因为这就是Select返回的内容。

您应该使用FirstOrDefault(),或者将返回类型更改为IQueryable(of ContentViewModel)。

于 2013-02-11T14:15:27.163 回答