lukik lukik - 5 months ago 34
Vb.net Question

Extract whole row/line using Lumenworks CSV Parser

How do I read the whole row while using

LumenWorks
CVS parser? So far am only able to read entry by entry but not the whole row. i.e. if a row is
a,b,c,d,e,f
, am able to pluck out each individual alphabet. However, I want to be able to read the whole row

Currently I have:

my_csv = New CsvReader(New StreamReader(file_path), False, ",", resetPoint)
field_count = my_csv.FieldCount
While my_csv.ReadNextRecord()
'process the data here
'This code will process each individual alphabet in one row


The above reads each individual alphabet. What I want is to have something like

row = my_csv.row


Is this option available or something similar?

'EDITED'

When you have basic VB programming skills like me, this is what you come up with to solve the problem

Dim my_string As String = ""
For x As Integer = 0 To my_csv.FieldCount - 1
my_string += my_csv(x).ToString.Trim + ","
Next
my_string = Mid(my_string, 1, Len(my_string) - 1)
Return my_string


By all means use the code in the marked answer. Its super elegant!

Answer

I haven't found anything available, but this should work:

VB:

Dim rowFields = Enumerable.Range(0, my_csv.FieldCount).
   Select(Function(field) my_csv(CInt(my_csv.CurrentRecordIndex), field))
Dim line As String = String.Join(my_csv.Delimiter.ToString(), rowFields)

C#:

var rowFields = Enumerable.Range(0, my_csv.FieldCount)
    .Select(field => my_csv[(int)my_csv.CurrentRecordIndex, field]);
string line = string.Join(my_csv.Delimiter.ToString(), rowFields);
Comments