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!
user3024750user3024750
asked Nov 2, 2016 at 16:30
1 Answer 1
Checks this out, it might spare you the effort:
It supports reading delimited CSV files:
answered Nov 2, 2016 at 16:36
3 Comments
user3024750
Thanks! Is there also a way without using an external library?
Timothy Ghanem
What you have done is basically what needs to be done. So, i think that's the best you can achieve.
Robert McKee
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.
lang-cs