dotnetdevcsharp
dotnetdevcsharp

Reputation: 3980

Query not reconizing custom properties

I Have the following query but its not allowing me to select the properties of my custom class for some reason.

This is my class here:

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


}

And here I want to use a query to access the data my end goal is to pass this object to a csv selrilizer which I have created to produce a csv file in a custom format.

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();

Upvotes: 0

Views: 32

Answers (1)

Kawsar Hamid
Kawsar Hamid

Reputation: 514

Try this way -

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

Using LinqToCsv you can write csv file from your linq object. LinqToCsv is available as nuget package.

From Package Manager Console -

 Install-Package LinqToCsv

Now you can write your linq object to csv file this way -

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);

Upvotes: 1

Related Questions