List(Of T) groupby visual basic

List(Of T) groupby visual basic

我想在 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

更新 使用 converter.telerik.com 由 Plutonix

建议
Dim moviesByCategories = movies.GroupBy(Function(x) x.Category).[Select](Function(x) New MovieCategory() With { _
Key .Title = x.Key, _
Key .Items = x.ToList() _
})

但是代码不起作用给出了以下错误

我的 class 1.电影页面视图模型

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
  1. 电影类别

    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
    

    更新 现在我在使用 Tom 提供的代码后收到此错误

查询语法(我更喜欢 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()
    })

不确定 "key" 在转换中的来源,这是正确的 VB 代码:

Dim moviesByCategories = movies.GroupBy(Function(x) x.Category)
                               .Select(Function(x) New MovieCategory() With {.Title = x.Key,
                                                                             .Items = x.ToList()}