我想按类别在gridview中分组电影。我有C#代码,我想将其转换为Visual Basic。
var moviesByCategories = movies.GroupBy(x => x.Category)
.Select(x => new MovieCategory { Title = x.Key, Items = x.ToList() });
我尝试了以下代码,但它给了我一个错误。
Dim query = From film In movies Group film.Title By Category = film.Category Into grpTitle = Group
UPDATE 使用了Plutonix建议的converter.telerik.com
Dim moviesByCategories = movies.GroupBy(Function(x) x.Category).[Select](Function(x) New MovieCategory() With { _
Key .Title = x.Key, _
Key .Items = x.ToList() _
})
但是代码没有工作会产生以下错误
我的课程 1. MoviePageViewModel
Public Class MoviesPageViewModel
Private _Items As List(Of MovieCategory)
Public Property Items() As List(Of MovieCategory)
Get
Return _Items
End Get
Set(ByVal value As List(Of MovieCategory))
_Items = value
End Set
End Property
Sub New()
Dim movies As List(Of Movie) = New List(Of Movie)
'adding movies to movies list
Dim moviesByCategories = movies.GroupBy(Function(x) x.Category).[Select](Function(x) New MovieCategory() With { _
Key .Title = x.Key, _
Key .Items = x.ToList() _
})
Items = moviesByCategories
End Sub
End Class
2.Movie class
Public Class Movie
Private _Title As String
Public Property Title() As String
Get
Return _Title
End Get
Set(ByVal value As String)
_Title = value
End Set
End Property
Private _Subtitle As String
Public Property Subtitle() As String
Get
Return _Subtitle
End Get
Set(ByVal value As String)
_Subtitle = value
End Set
End Property
Private _Image As String
Public Property Image() As String
Get
Return _Image
End Get
Set(ByVal value As String)
_Image = value
End Set
End Property
Private _Category As String
Public Property Category() As String
Get
Return _Category
End Get
Set(ByVal value As String)
_Category = value
End Set
End Property
Sub New(title As String, category As String, subtitle As String, img As String)
_Title = title
_Subtitle = subtitle
_Image = img
_Category = category
End Sub
End Class
MovieCategory
Public Class MovieCategory
Private _Title As String
Public Property Title() As String
Get
Return _Title
End Get
Set(ByVal value As String)
_Title = value
End Set
End Property
Private _Items As List(Of Movie)
Public Property Items() As List(Of Movie)
Get
Return _Items
End Get
Set(ByVal value As List(Of Movie))
_Items = value
End Set
End Property
End Class
UPDATE 现在我使用Tom提供的代码后出现此错误
答案 0 :(得分:4)
查询语法(我更喜欢VB.NET):
Dim query = From movy In movies
Group By category = movy.Category Into MovyCategoryGroup = Group
Select New MovieCategory With {
.Title = category,
.Items = MovyCategoryGroup.ToList()
}
方法语法:
Dim query = movies.GroupBy(Function(m) m.Category).
Select(Function(g) New MovieCategory With {
.Title = g.Key,
.Items = g.ToList()
})
答案 1 :(得分:2)
不确定转换中“key”的来源,这是正确的VB代码:
Dim moviesByCategories = movies.GroupBy(Function(x) x.Category)
.Select(Function(x) New MovieCategory() With {.Title = x.Key,
.Items = x.ToList()}