2014-09-25 120 views
0

我試圖遍歷這個列表並獲得一次一個醫生的值。通過列表進行迭代

在excel文檔中,每個醫生都有多個醫生,但我希望輸出是一個醫生。

在excel文檔中,它會出現HILL CANN CANN HILL,這就是我現在回來的。

但我想每個醫生迭代所以它會去山連山CANN CANN

 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 
+0

您是否需要*在*中閱讀它們,並在閱讀時打印每個項目,或者您可以閱讀所有內容,進行排序,然後輸出每個項目?另外我猜這是作業。 – helrich 2014-09-25 17:54:31

+0

好吧,我將最終在Excel文件中添加一些其他信息。所以我認爲通過這樣的每位醫生迭代會給我提供每名醫生每PER時間的信息。不,不是功課。只是想擴大我對vb的瞭解 – FoxMcCloud 2014-09-25 18:00:24

回答

1

下面是使用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 

這個例子並不直接解決您的問題在分類複製和粘貼 - 到 - 你的項目的時裝,但它說明了幾種方法通過一個普通的列表進行迭代。

+0

好吧,非常感謝!我肯定會看看LINQ。 – FoxMcCloud 2014-09-25 18:54:32