gpt4 book ai didi

vb.net - List(Of T) groupby visual basic

转载 作者:行者123 更新时间:2023-12-04 08:24:04 25 4
gpt4 key购买 nike

我想按类别对 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

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

但是代码不起作用会出现以下错误
enter image description here

我的类(class)
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.电影类
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
  • 电影类别
    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 提供的代码后收到此错误
    enter image description here
  • 最佳答案

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

    关于vb.net - List(Of T) groupby visual basic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28090835/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com