作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想按类别对 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
Dim moviesByCategories = movies.GroupBy(Function(x) x.Category).[Select](Function(x) New MovieCategory() With { _
Key .Title = x.Key, _
Key .Items = x.ToList() _
})
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
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
最佳答案
查询语法(我更喜欢在 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/
我是一名优秀的程序员,十分优秀!