List(Of T) groupby visual basic

拟墨画扇 提交于 2019-12-11 03:13:57

问题


i want to Group Movies in gridview by category. i have C# code and i want to convert it to Visual Basic.

var moviesByCategories = movies.GroupBy(x => x.Category)
            .Select(x => new MovieCategory { Title = x.Key, Items = x.ToList() });

i tried the following code but it gives me an error.

Dim query = From film In movies Group film.Title By Category = film.Category Into grpTitle = Group

UPDATE used converter.telerik.com suggested by Plutonix

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

But the code doesn't work gives the following error

My classes 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
  1. 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 now i get this error after using the code provided by Tom


回答1:


Query syntax (which i prefer in 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()
    }

Method syntax:

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



回答2:


Not sure where the "key" came from in the conversion, this is proper VB code:

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


来源:https://stackoverflow.com/questions/28090835/listof-t-groupby-visual-basic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!