如何将ListItemCollection转换为List(对象)

时间:2012-07-26 10:33:10

标签: asp.net vb.net

我已经定义了一个类

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

如何将ListItemCollection转换为(MyClass)列表?

2 个答案:

答案 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