我正在使用linq到csv输出实体到csv文件。我已经循环了源,并且在PersonalDetails
变量中肯定有六条记录。
public class PersonalDetails
{
public string LineType { get; set; }
public string EnquirerTitle { get; set; }
public string ForeName { get; set; }
public string Surname { get; set; }
public int? Age { get; set; }
public DateTime? DateOfBirth { get; set; }
public string MaritalStatus { get; set; }
public string HomePhone { get; set; }
public string MobilePhone { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public string Employment { get; set; }
public string Occupation { get; set; }
}
public Boolean CsvOutput(string filename, char delimnator)
{
try
{
CsvFileDescription outputFileDescription = new CsvFileDescription
{
SeparatorChar = delimnator, // tab delimited
FirstLineHasColumnNames = false, // no column names in first record
EnforceCsvColumnAttribute = true,
FileCultureName = "nl-NL" // use formats used in The Netherlands
};
CsvContext cc = new CsvContext();
IQueryable<tblapertureNetAppointment> _personadetails;
var personalDetails = (from _appointments in _dal.apertureNetEntities.tblapertureNetAppointments
select new PersonalDetails()
{
LineType = "Details",
EnquirerTitle = "MR",
ForeName = _appointments.CustomerFirstName,
Surname = _appointments.CustomerLastName,
Age = _appointments.age,
DateOfBirth = _appointments.dob,
MaritalStatus = "Single",
HomePhone = "MobilePhone",
Email = "david.buckley@aperture.uk.com",
Address = _appointments.Address1 + "," + _appointments.Address2 + "," + _appointments.County + "," + _appointments.PostCode,
Employment = "Developer",
Occupation = "Yes"
}).ToList();
cc.Write(personalDetails,filename,outputFileDescription);
}
catch (Exception ex)
{
return false;
}
return true;
}
创建文件,但实际文件中没有数据。当我调试有记录在PersonalDetails
下面是编写csv的简单方法
StreamWriter writer = new StreamWriter(filename);
string[][] rows = {
{"abc", "cdf", "ghi"},
{"jkl", "mno", "pqr"},
{"stu", "vwx", "yz"}
};
foreach (string[] row in rows)
{
writer.WriteLine(string.Join(",", row));
}
writer.Flush();
writer.Close();