user3024750 user3024750 - 1 month ago 9
C# Question

Make importing a CSV string into a List of a class easier

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!

Answer

Checks this out, it might spare you the effort:

FileHelpers

It supports reading delimited CSV files:

Read Delimited File