CSV to object model mapping

后端 未结 6 662
礼貌的吻别
礼貌的吻别 2020-12-01 07:51

I have a CSV file that I want to read into a List. Here\'s an example file:

Plant,Material,\"Density, Lb/ft3\",Storage Location
FRED,10000477,64.3008,3300
F         


        
6条回答
  •  遥遥无期
    2020-12-01 08:21

    With Cinchoo ETL library, can parse CSV file into objects

    Define type matching CSV file structure as below

    public class PlantType
    {
        public string Plant { get; set; }
        public int Material { get; set; }
        public double Density { get; set; }
        public int StorageLocation { get; set; }
    }
    

    Load the CSV file

    static void LoadCSV()
    {
        using (var p = new ChoCSVReader("*** YOUR CSV FILE PATH ***")
            .WithFirstLineHeader(true)
            )
        {
            foreach (var rec in p)
            {
                Console.WriteLine(rec.Dump());
            }
        }
    }
    

提交回复
热议问题