I’ve got some large csv files that I need to import into IEnumerable (prob a list) so that I can do some “magic” on them before saving into a db. I don’t need every value (column) from the csv.
However, I can’t find a better alternative than this:
Read csv file by line
Split the line on ,
new MyObj{
Prop1 = split[0],
Prop2 = split[1],
Prop3 = split[6],
Prop4 = split[7],
Prop5 = split[9]
}
Add new MyObj to List
This works and is quick enough, but seems very clunky?
Is there an alternative (other than add a ctor, which acheives the same as above).
You can use a CSV parser – there is one in the
Microsoft.VisualBasic.FileIOnamespace – theTextFieldParser.FileHelpers is another popular option, and there are many free ones around (just search).