我已经定义了一个类
Public Class MyClass
Public Type1 As Integer
Public Type2 As float
End Class
如何将ListItemCollection转换为(MyClass)列表?
答案 0 :(得分:1)
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()
答案 1 :(得分:1)
MyClass
是关键字,VB.NET中的float
类型通过Single
别名(System.Single)表示
Public Class Item
Public Type1 As Integer
Public Type2 As Single
End Class
我使用 sample 数据构建了集合。
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