我正在使用 CsvHelper 并能够解析 csv 文件。我的问题是如何解析Date
intoDateTime
对象
我想在解析 csv 而不是迭代集合时通过 CsvHelper 转换它
public static List<StockModel> SplitCsv(string csv)
{
var textReader = new StringReader(csv);
var csvr = new CsvReader(textReader);
csvr.Configuration.RegisterClassMap<ModelMap>();
var records = csvr.GetRecords<StockModel>().ToList();
return records;
}
public class StockModel
{
public string Date { get; set; } // I want this object to be DateTime
public string Base { get; set; }
public string Open { get; set; }
}
public sealed class ModelMap : CsvClassMap<StockModel>
{
public ModelMap()
{
Map(m => m.Date);
Map(m => m.Base);
Map(m => m.Open);
}
}
CSV 示例
Date,Base,Open
2016-02-29,1437.530029,1445.839966
2016-02-25,1431.439941,1431.439941
2016-02-24,1430.459961,1432.430054