简体   繁体   English

如何获取 VB.NET 中的列表值? (GridView 运行良好,但我需要访问每个值)

[英]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这是我的方法,如果我将它绑定在 GridView 中,效果很好,如下所示: 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....)但不是使用 GridView,我需要遍历列表值(如果标签值是例如 23 则稍后应用条件......)

So I did the following but unfortunately this prints tag;tag;tag;tag;tag;tag;所以我做了以下但不幸的是这个打印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这是来自@41686d6564standsw.Palestine 的正确答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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