2

我已经定义了一个类

Public Class MyClass
 Public Type1 As Integer
 Public Type2 As float
End Class

如何将 ListItemCollection 转换为 list(of MyClass) ?

4

2 回答 2

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()
于 2012-07-26T10:35:54.317 回答
1

VB.NET 中的MyClassis 关键字和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
于 2012-07-26T11:08:22.547 回答