user3024750 user3024750 - 1 year ago 53
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)
.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.


Answer Source

Checks this out, it might spare you the effort:


It supports reading delimited CSV files:

Read Delimited File