我不太清楚如何做到这一点,但我试图逐行读取.CSV文件,将每条独立行存储到列表<>中。然后遍历列表并使用每个项目(行)作为Employee类的新实例的签名。C#读文件行到列表中,并作为参数使用
database.csv
John Doe,4568,0
Jane Smith,6154,1 // this represents my constructors signature
...
...
Employee.cs(唯一的构造)
public Employee(string emp, string id, int state)
{
EmpName = emp;
IdName = id;
_empState = state;
}
我的方法库中存储类:
public IList<Employee> getListOfActiveEmployees()
{
string filePath = @"(filepath)\database.csv";
var emp = new List<Employee>();
//var LogFile = File.ReadAllLines(filePath);
List<string> Lines = new List<string>();
using (var sr = new StreamReader(filePath))
{
while (sr.Peek() >= 0)
{
Lines.Add(sr.ReadLine());
}
}
foreach (var row in Lines)
{
emp += new Employee(row);
}
return emp;
}
错误:
Error CS7036 There is no argument given that corresponds to the required
formal parameter 'id' of 'Employee.Employee(string, string, int)'
我的猜测是,它的读取整条生产线为我的第一个输入?有没有办法做我想要完成的事情?
太棒了,而且会消除其他for-loop。现在它是有道理的,每一行都被当作一个字符串来读取,所以它当然会被传递到第一个参数中。谢谢John,我会阅读Split()函数。 – 2014-12-05 05:53:11