首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >遍历列表

遍历列表
EN

Stack Overflow用户
提问于 2014-09-26 01:48:32
回答 1查看 411关注 0票数 0

我正试图遍历这个列表,并一次获得一个医生的值。

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

在excel文档中,它是HILL CANN HILL,这就是我现在要返回的内容。

但是我想迭代每个医生,所以它会去HILL CANN CANN

代码语言:javascript
复制
        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
EN

回答 1

Stack Overflow用户

发布于 2014-09-26 02:18:50

这里有一些使用LINQ的例子。我强烈建议您阅读它,因为其中有一些强大的东西。学习LINQ以及lambda表达式和匿名方法/函数会非常有帮助!

代码语言:javascript
复制
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

这个示例并没有以一种复制粘贴到项目中的方式直接解决您的问题,但是它演示了几种遍历泛型列表的方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26044952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档