I am new to Linq. I have to use Linq to Sql to project data from sql server to a complex object. The complex object is the Employee class which has another class, EmployeeDetail, as a property. I am able to populate the Employee class but unable to populate the EmployeeDetail. I have enclosed my code below. Obviously, I have to instantiate EmployeeDetail within the select new Employee block and attach it. Unfortunately, I don't know how to do it.
public class Employee
{
public String Email { get; set; }
public String EmployeeId { get; set; }
public EmployeeDetail employeeDetail { get; set; }
}
public class EmployeeDetail
{
public String PhoneNumber { get; set; }
public String Address { get; set; }
}
public class Data
{
public List<Employee> GetEmployee()
{
EmployeeDataContext dbDontext = new EmployeeDataContext ();
var employees = from i in dbDontext.Employee
join m in dbDontext.Employee on i.Employee_Id equals
m.Emplyee_Detail_Id
select new Employee
{
Email = i.email,
EmployeeId = i.employee_Id.ToString(),
//how to get data for the EmployeeDetail property
//with PhoneNumber and Address values populated?
};
var employeeList = employees.ToList();
return employeeList;
}
}