Sub NoNonsenseCSV2DataTable(CSVfile As String)
'A,B,C,D,E
'00001,4,1,2,3560
'00002,4,12,1,2000
'00003,1,1,2,4500
'00004,4,12,1,2538.63
'00005,1,1,2,3400
'00006,1,1,2,2996.48
Dim dTable As New DataTable
Using reader As New StreamReader(CSVfile)
'_______________________________________________________________________
Dim CSVheader As String = reader.ReadLine
Dim Cols = (From s In CSVheader.Split(",") Select s).ToList()
Dim setTblColumns = (From s In Cols Select dTable.Columns.Add(s, GetType(String))).ToList
'TEST Dim getTblColumns As List(Of String) = (From c As DataColumn In dTable.Columns Select c.ColumnName).ToList()
'_______________________________________________________________________
Dim ReadToEnd As String = reader.ReadToEnd()
Dim Rows = (From s In ReadToEnd.Split(vbLf) Select s).ToList()
' up to now everything goes well
Dim getTblRows = (From z In Rows Select z.Split(",")).ToList()
' getting DataTable rows, OK
'xxx Dim x As DataRow
'xxx For Each s As String In Rows
'xxx x = dTable.NewRow()
'xxx x.ItemArray = s.Split(","c)
'xxx dTable.Rows.Add(x)
'xxx Next
'_______________________________________________________________________
End Using
End Sub
What I have tried:
The procedure works 100% but I want to change the last part, so I would get an advantage of LINQ: nice looking code and better performance. Thanks for help in advance
ATeDe
Dim x As DataRow
For Each s As String In Rows
x = dTable.NewRow()
x.ItemArray = s.Split(","c)
dTable.Rows.Add(x)
Next