我有以下查询,但由于某种原因,它不允许我选择自定义类的属性。
这是我的班级:
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();
尝试这种方式 -
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();
update
使用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);