我已经定义了一个类
Public Class MyClass
Public Type1 As Integer
Public Type2 As float
End Class
如何将 ListItemCollection 转换为 list(of MyClass) ?
Dim x As New List(of [MyClass])
For Each y as [MyClass] in ListItemCollectionInstance.Items
if (y.Selected)
x.Add(y)
Next y
使用 Linq:
Dim selectedItems As List(Of [MyClass]) = ListItemCollectionInstance.Items.Cast(Of [MyClass])().Where(Function(itm) itm.Selected = True).ToList()
VB.NET 中的MyClass
is 关键字和float
类型通过Single
别名 (System.Single)表示
Public Class Item
Public Type1 As Integer
Public Type2 As Single
End Class
我已经用示例数据构建了集合。
Dim cl As New ListItemCollection
cl.Add(New ListItem("1", "1.2"))
cl.Add(New ListItem("2", "2.3"))
cl.Add(New ListItem("3", "3.4"))
Dim lst = From ele In cl Select New _
Item With
{
.Type1 = Integer.Parse(ele.Text),
.Type2 = Single.Parse(ele.Value)
}
For Each ele As Item In lst
Response.Write("<br/> " & ele.Type1 & " " & ele.Type2)
Next