我有一个以下csv文件:
Employer EID,File Creation Date,File Creation Time,Salary Year and Month,Total Salaries,Total Records,,,
1006200,20032016,1031,201603,2200,1,,,
Record ID,Employee QID,Number of Working Days,Net Salary,Basic Salary,Extra hours,Extra Income,Deductions,Payment Type
1,29135617946,31,2200,2200,0,0,0,SALARY
2,29135617947,31,2200,2300,0,0,0,SALARY
3,29135617948,31,2200,2250,0,0,0,SALARY
上面的文件是第一部分作为雇主信息,以及雇员与其一起工作的行的行和工资。
我想创建一个复杂的类Employer
,它将拥有employees属性以及名为List
的{{1}}复杂类型,其中包含所有员工信息。
请通过提供方向/代码来帮助我如何使用FileHelper实现此目的。
答案 0 :(得分:0)
您可以使用MasterDetailEngine
。有an example in the documentation。
您提供选择器类,以使FileHelpers能够确定记录是主记录还是详细记录。
private RecordAction ExampleSelector(string record)
{
if (record.Length < 2)
return RecordAction.Skip;
if (IsMasterRecord(record)) // some way of identifying your master records
return RecordAction.Master;
else
return RecordAction.Detail;
}
然后你可以将你的文件读到这样的课程中:
var engine = new MasterDetailEngine<Employer, Employee>(new MasterDetailSelector(ExampleSelector));
var result = engine.ReadFile("Input.txt");
foreach (var group in result) {
Console.WriteLine("Customer: {0}", group.Master.EmployerEid);
foreach (var detail in group.Details)
Console.WriteLine(" Freight: {0}", detail.EmployeeQid);
}