简体   繁体   中英

How to get a List values in VB.NET? (GridView works well but I need to access each value instead)

Here's my method which works very well if I bind it in a GridView as follows: GridView1.DataSource = Me.GetTags

Partial Class backoffice_posts_Edit
    Private Function GetTags() As List(Of Tag)
        Dim constring As String = ConfigurationManager.ConnectionStrings("AccessConnectionString1").ConnectionString
        Using con As New SqlConnection(constring)
            Using cmd As New SqlCommand("SELECT tag_id FROM tags_tbl WHERE tag_choice = 1", con)
                Dim tags As New List(Of Tag)
                cmd.CommandType = CommandType.Text
                con.Open()
                Using sdr As SqlDataReader = cmd.ExecuteReader()
                    While sdr.Read()
                        tags.Add(New Tag With {
                                      .tagId = Convert.ToInt32(sdr("tag_id"))
                                    })
                    End While
                End Using
                con.Close()
                Return tags
            End Using
        End Using
    End Function
End Class

Public Class Tag
    Public Property tagId As Integer
End Class

But instead of using GridView, I need to loop through the List values (to later apply a condition if a tag value is for example 23....)

So I did the following but unfortunately this prints tag;tag;tag;tag;tag;tag; and not the values:

For Each element In Me.GetTags
    Response.Write(element)
    Response.Write(";")
Next
For Each element In Me.GetTags
    Response.Write(element.tagId)
    Response.Write(";")
Next

or

For Each tagId In Me.GetTags().Select(Function(t) t.tagId)

This is the correct answer from @41686d6564standsw.Palestine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM