0

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 ?

Nkosi
  • 191,971
  • 29
  • 311
  • 378
seesharp
  • 23
  • 7
  • 5
    duplicate with this one http://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a-linq-to-entities-query – DarkMakukudo Jun 10 '16 at 06:33

1 Answers1

0

why select an entity?

 var students = db.Students.Include("Courses").ToList();

If you use this option just to have a detached idem, you can append .AsNoTracking() or disable LazyLoading and proxy observable construsction in the DbContext options before use it.

If you need to modify it you can use a dynamic:

var students = db.Students.Include("Courses").Select(p => new { /* ... */ }).ToList().Select(p => new Student { /* ... */ }).ToList();

Otherwhise you can use Automapper to avoid this code and create an association between your entity model StudentDTO and your business (or viewmodel) Student class, that will map each property named similar. If you need to map two similar class you can change the model.tt to create a specular detached class and map it together.

Another way to transform data before use it is the extension .Project() in , that let filter the fields before get them in memory,.

Luca Mazzanti
  • 529
  • 4
  • 10
  • In `var students = db.Students.Include("Courses").Select(p => new { /* ... */ }).ToList().Select(p => new Student { /* ... */ }).ToList();` what properties should be included in the first Select ? – seesharp Jun 13 '16 at 07:15
  • You create a dynamic temporary class with all the properties you want to pick from sql. You can't to complex operation because they must be convertible in sql by EF IQueryable. Probably you want to pick all properties from Student, to rebind them in the real Student class. This solution is not a good one because it's error prone. You write two times each property associations manually. In this case there is also a nested item list to replicate. I think it is more interesting to use Automapper and generate two identical class in the model.tt – Luca Mazzanti Jun 13 '16 at 14:35