我有一个CSV文件,格式如下:名字,姓氏,出生日期,死亡日期。如何从CSV文件中获取所有数据并将其转换为对象列表?
我正在考虑实施以下课程
public class Person
{
private string f_name;
private string l_name;
private int dob;
private int dod;
public Person(string first, string second, int dob, int dod)
{
this.f_name = first;
this.l_name = second;
this.dob = dob;
this.dod = dod;
}
}
答案 0 :(得分:-1)
List<Person> result = File.ReadAllLines("@C:\file.csv")
.Select(y => y.Split(','))
.Select(x => new {
first = x[0],
second = x[1],
dob=int.Parse(x[2]),
dod =int.Parse(x[3]))
}).Select(x=> new Person(x.first, x.second, x.dob, x.dod ))
.ToList();
的启发