Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 遍历列表_Vb.net - Fatal编程技术网

Vb.net 遍历列表

Vb.net 遍历列表,vb.net,Vb.net,我试图遍历这个列表,每次获取一个医生的值 excel文档中每个医生有多个,但我希望每次输出一个医生 在excel文档中,它是HILL CANN HILL,这就是我目前得到的结果 但我想对每个医生进行迭代,这样就可以顺利完成 Dim physname As New List(Of String)() 'Get all of the data from tblPhysician that you will use and put into a list for search

我试图遍历这个列表,每次获取一个医生的值

excel文档中每个医生有多个,但我希望每次输出一个医生

在excel文档中,它是HILL CANN HILL,这就是我目前得到的结果

但我想对每个医生进行迭代,这样就可以顺利完成

        Dim physname As New List(Of String)()

    'Get all of the data from tblPhysician that you will use and put into a list for searching the excel file
    Sql = "select * from tblPhysician "
    conn = New OdbcConnection(connectionString)
    conn.Open()
    comm = New OdbcCommand(Sql, conn)
    dr = comm.ExecuteReader()

    'Populate the physname list with the doctors names
    While (dr.Read())
        physname.Add(dr("PhysicianName").ToString())
    End While

range = oxlsheet.UsedRange
    For rcnt = 1 To range.Rows.Count
        For ccnt = 2 To 6
            varray = CType(range.Cells(rcnt, ccnt), Excel.Range)
            If (IsNumeric(varray.value)) Then
                temp = varray.value.ToString
            Else
                temp = varray.value
            End If

            'Iterate through physname list for each doctor in the list
            For Each doctor As String In physname

                If (rcnt > 8) Then
                    If (IsNumeric(varray.Columns(4).value)) Then
                        temp2 = varray.Columns(4).value.ToString
                    Else
                        temp2 = varray.Columns(4).value
                    End If

                    'If the name in the excel column matches, write out the name and value
                    If (temp2 = doctor) Then
                        Console.WriteLine(varray.Columns(4).value)
                        Console.WriteLine(varray.Columns(5).value)
                        Console.ReadLine()
                    End If


                End If

            Next

        Next
    Next

下面是一些使用LINQ的示例。我强烈建议你仔细阅读,因为里面有一些强大的东西。学习LINQ以及lambda表达式和匿名方法/函数非常有帮助

Imports System
IMports System.Collections.Generic
Imports System.Linq

Public Module Module1

    Public Sub Main()
        Dim physname as new List(Of String) From {"HILL", "CANN", "CANN", "HILL"}

        ' Output the raw list as a baseline...
        Console.WriteLine("Raw list of physicians:")
        For Each physician as string in physname
            Console.WriteLine(physician)
        next

        ' Using Distinct will select each physician once...
        Console.WriteLine()
        Console.WriteLine("Distinct physicians:")
        For Each physician as String in physname.Distinct()
            Console.WriteLine(physician)
        next

        ' Sort the list in place and then display...
        Console.WriteLine()
        Console.WriteLine("Raw physicians list ordered:")
        physname.Sort() 
        For Each physician as String in physname
            Console.WriteLine(physician)
        next
    End Sub
End Module

本例并没有以复制粘贴到项目中的方式直接解决您的问题,但它演示了在通用列表中迭代的几种方法。

您是否需要按顺序读取并在读取时打印每个项目,或者您是否可以读取所有内容、排序,然后输出每个项目?我猜这是家庭作业。我最终会在excel文件中添加一些其他信息。所以我认为像这样迭代每个医生会给我每次列表中每个医生的信息。不,不是作业。只是想拓宽我的vb知识