1

I have 3 CSV files which I download and read into a List of a class that matches the CSV file. Now I do that using a LINQ query. Code:

 var ListOfCSV= CsvString.Remove(CsvString.LastIndexOf((Environment.NewLine), StringComparison.Ordinal)).Split(new[] { Environment.NewLine }, StringSplitOptions.None)
 .Skip(1)
 .Select(columns => columns.Split(';'))
 .Select(columns => new MyClass
 {
 argument1 = columns[0],
 argument2 = columns[1],
 argument3 = columns[2],
 argument4 = (columns[3]),
 argument5 = columns[4],
 argument6 = columns[5],
 argument7 = columns[6],
 argument8 = columns[7],
 });

I do that 3 times for each CSV file (as they are different classes). Is there a way to shorten this or maybe make it faster? It is certainly not slow but I just would like to make it as fast and well performing as possible.

Thanks!

asked Nov 2, 2016 at 16:30

1 Answer 1

3

Checks this out, it might spare you the effort:

FileHelpers

It supports reading delimited CSV files:

Read Delimited File

answered Nov 2, 2016 at 16:36

3 Comments

Thanks! Is there also a way without using an external library?
What you have done is basically what needs to be done. So, i think that's the best you can achieve.
I highly recommend FileHelpers. It just solves so many edge-case issues that I find it just isn't worth re-inventing that wheel ever again.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.