我有以下嵌套视图模型:
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”