查询未重新调整自定义属性

时间:2016-06-29 15:04:55

标签: c# asp.net

我有以下查询但它不允许我出于某种原因选择我的自定义类的属性。

这是我的班级:

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 Dob { 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; }


}

在这里,我想使用查询来访问数据,我的最终目标是将此对象传递给我创建的csv selrilizer,以生成自定义格式的csv文件。

IQueryable<tblapertureNetAppointment> _personadetails;
var personalDetails = from _appointments in  _dal.apertureNetEntities.tblapertureNetAppointments
.AsEnumerable()
.Select(x => new PersonalDetails { x.LineType its not allowing me to find line type})
.ToList();

1 个答案:

答案 0 :(得分:1)

尝试这种方式 -

var personalDetails = (from _appointments in  _dal.apertureNetEntities.tblapertureNetAppointments.AsEnumerable()
                       select new PersonalDetails {

                         LineType = _appointments.LineType,
                         EnquirerTitle = _appointments.EnquirerTitle,
                         ForeName = _appointments.ForeName,
                         Surname = _appointments.Surname,
                         // .......
                       }).ToList();

<强>更新

使用LinqToCsv,您可以从linq对象编写csv文件。 LinqToCsv以nuget包的形式提供。

从包管理器控制台 -

 Install-Package LinqToCsv

现在你可以用这种方式将linq对象写入csv文件 -

CsvFileDescription outputFileDescription = new CsvFileDescription
{
    SeparatorChar = '\t', // tab delimited
    FirstLineHasColumnNames = true, 
    FileCultureName = "nl-NL" // use formats used in The Netherlands
};

CsvContext cc = new CsvContext();

string fileName = String.Format(@"{0}products2.csv", Server.MapPath("/csvFiles")); 
cc.Write(personalDetails,fileName,outputFileDescription);