vb.net List(Of T) groupby visual basic

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28090835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:43:06  来源:igfitidea点击:

List(Of T) groupby visual basic

vb.netwindows-store-apps

提问by Junius L.

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

我想按类别在 gridview 中对电影进行分组。我有 C# 代码,我想将其转换为 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

更新使用了 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() _
})

But the code doesn't work gives the following error enter image description here

但是代码不起作用会出现以下错误 在此处输入图片说明

My classes 1. MoviePageViewModel

我的课程 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

2.电影课

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 enter image description here

  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 提供的代码后收到此错误 在此处输入图片说明

回答by Tim Schmelter

Query syntax (which i prefer in VB.NET):

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

回答by Drunken Code Monkey

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

不确定转换中“密钥”的来源,这是正确的 VB 代码:

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