我有以下嵌套的视图模型:
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
我的问题是我无法返回填充的viewmodel,因为我总是得到错误:
Type" System.Data.Objects.ObjectQuery`1 [MvcApp.ContentViewModel]"的对象无法转换为" MvcApp.ContentViewModel"
答案 0 :(得分:0)
您的函数正文创建IQueryable
ContentViewModel
(实现为System.Data.Objects.ObjectQuery`1[MvcApp.ContentViewModel]
),因为这是Select
返回的内容。
您应该使用FirstOrDefault()
,或将返回类型更改为IQueryable
(ContentViewModel
)。