CSV to object model mapping

后端 未结 6 670
礼貌的吻别
礼貌的吻别 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:20

    You can use a simple code like this, which ignores the header and doesn't work with quotes, but may be sufficient for your needs.

    from line in File.ReadAllLines(fileName).Skip(1)
    let columns = line.Split(',')
    select new
    {
      Plant = columns[0],
      Material = int.Parse(columns[1]),
      Density = float.Parse(columns[2]),
      StorageLocation = int.Parse(columns[3])
    }
    

    Or you can use a library, like others suggested.

提交回复
热议问题