Linq to CSV不输出数据

时间:2016-07-01 12:55:16

标签: c# linq csv

我正在使用linq to 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中有记录

1 个答案:

答案 0 :(得分:0)

这是编写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();