I have a project which is an Asp.net Web API.
Here's my controller :
public IEnumerable<Students> GetBasicInfo()
{
var students = db.Students.Include("Courses").Select(p => new Students
{
student_id = p.student_id,
student_number = p.student_number,
first_name = p.first_name,
middle_name = p.middle_name,
last_name = p.last_name,
display_name = p.display_name,
address = p.address,
birthdate = p.birthdate,
contact_number = p.contact_number,
email = p.email,
student_img = p.student_img,
courses = p.courses.Select(a => new Courses
{
course_id = a.course_id,
name = a.name
})
}).ToList();
return students;
}
Here's my model
Students.cs
public partial class Students
{
public System.Guid student_id { get; set; }
public string student_number { get; set; }
public string first_name { get; set; }
public string middle_name { get; set; }
public string last_name { get; set; }
public string display_name { get; set; }
public string address { get; set; }
public System.DateTime birthdate { get; set; }
public Nullable<int> course_id { get; set; }
public string contact_number { get; set; }
public string email { get; set; }
public byte[] student_img { get; set; }
public IEnumerable<Courses> courses { get; set; }
}
Here's the Courses.cs
public partial class Courses
{
public int course_id { get; set; }
public string name { get; set; }
}
However it returns an error which says
'The entity or complex type 'StudentModel.Studentss' cannot be constructed in a LINQ to Entities query.'.
Any idea why ?