2016-02-23 43 views
1

我正在撰寫一個計劃來幫助我們的人力資源部門進入工資單。 SQL查詢輸出包含這樣的數據文件:如何隔離列表項以執行其他數據操作?

1,E,REG,40.0000000 
10,E,REG,11.5000000 
10,E,REG,11.0000000 
10,E,REG,5.5000000 
10,E,REG,.0000000 
10,E,REG,2.5000000 
10,E,REG,3.5000000 
10,E,REG,6.0000000 
10,E,OT,5.5000000 
10,E,OT,9.5000000 

在VB中,我創建了一個名爲Employee類

Public Class Employee 
    Public Property empNo As Integer 
    Public Property eCode As String 
    Public Property webCode As String 
    Public Property hrsWork As Decimal 

End Class 

我已經能夠採取從文件加載信息它變成一個列表

Dim allEmployees = From line In System.IO.File.ReadLines("C:\file\testout.txt") 
         Let Columns = line.Split(","c) 
         Where Columns.Length = 4 
         Let empNum = Integer.Parse(Columns(0).Trim()) 
         Let eCode = Columns(1) 
         Let webCode = Columns(2) 
         Let hrsWrk = Decimal.Parse(Columns(3)) 
         Select New Employee With {.empNo = empNum, .eCode = eCode, .webCode = webCode, .hrsWork = hrsWrk} 
Dim EmpList As List(Of Employee) = allEmployees.ToList() 

我下一步需要做什麼,就是通過這個列表,並以數字取個別員工,再由網絡代碼(REG,OT等)和第總和埃爾小時。完成後,我需要將它們輸出回txt文件,HR可以將其導入我們的工資系統。

輸出文件需要看起來像這樣:

1,E,REG,40.0000000 
10,E,REG,40.0000000 
10,E,OT,15.0000000 

等整個文件。

我真的停留在如何隔離每個員工以繼續。 有什麼建議嗎?

+0

我應該認爲,如果您使用循環和字典(Int32,Employee),您可以在讀取文件時添加Reg和OT值 – Plutonix

+0

您是否有示例代碼片段或指向其他站點的鏈接我可以參考嗎? –

回答

0

您可以在使用循環讀取數據時彙總數據。首先,使循環簡單,因爲個人喜好,我做了Employee類足夠聰明,創造本身:

Public Class Employee 
    ...properties 
    Public Sub New(items As String()) 
     empNo = Int32.Parse(items(0)) 
     eCode = items(1) 
     webCode = items(2) 
     hrsWork = Decimal.Parse(items(3)) 
    End Sub 
End Class 

代碼分割數據傳遞給它,讓它分配值。然後循環:

Dim eList As New List(Of Employee) 
Dim line As String 
Dim data() As String 
Dim EmpId As Int32 

Using sr As New StreamReader("C:\Temp\empsql.csv") 
    Do Until sr.EndOfStream 
     line = sr.ReadLine 
     data = line.Split(","c) 

     EmpId = Int32.Parse(data(0)) 
     ' find an emp with matching number and this "webcode" 
     Dim oldemp = eList. 
       Where(Function(w) w.empNo = EmpId AndAlso w.webCode = data(2)). 
       FirstOrDefault() 

     ' if emp does not exist, create one 
     If oldemp Is Nothing Then 
      Dim Emp = New Employee(data) 
      eList.Add(Emp) 
     Else 
      ' add the current hours 
      oldemp.hrsWork += Decimal.Parse(data(3)) 
     End If 
    Loop 
End Using 

另外,您可以處理您創建的原始數據,螺母的集合,而不是創造,這將只需要在後面取出物品,一個額外的步驟,讓你遞增小時已經閱讀任何記錄。測試結果:

For Each emp In eList 
    Console.WriteLine("{0} {1} {2} {3}", emp.empNo, emp.eCode, 
         emp.webCode, emp.hrsWork) 
Next 

結果:

1 E REG 40.0000000 
10 E REG 40.0000000 
10 E OT 15.0000000 

把它寫回來了,你可以做同樣的事情,不同的是加入逗號格式字符串和寫入文件,而不是調試窗口。

請注意,String.Split不適用於解析CSV。它可能會在幾個常見的事情上失敗。該TextFieldParser更強大,請參閱鏈接的例子。鑑於你的來源和內容可能是安全的,但值得一提的是其他人。

+0

太棒了!它很棒!謝謝您的幫助!現在讓它輸出迴文件。 –

0

我認爲你正在尋找的是一個FOR EACH循環。

Using myWriter as new system.io.streamwriter("<Path to your output file>") 
    For Each tempemployee as Employee in EmpList 
     mywriter.writeline(tempemployee.empno, tempemployee.eCode, tempemployee.webCode, tempemployee.hrsWork) 
    Next 
End Using 

您上面的代碼將使用您正在查找的輸出格式編寫一個文本文件。爲了解釋,循環的每次迭代都會使tempemployee成爲一個新的價值.....例如,如果您有3名員工,循環的第一遍將使tempemployee成爲第一個員工,然後第二次,tempemployee現在第二個,然後是第三個。

相關問題