Accessing the relationship of a relationship with Entity Framework

Posted by J. Pablo Fernández on Stack Overflow See other posts from Stack Overflow or by J. Pablo Fernández
Published on 2009-09-13T16:42:02Z Indexed on 2010/04/07 2:03 UTC
Read the original article Hit count: 441

Filed under:

I the School class I have this code:

from student in this.Students where student.Teacher.Id == id select student

The Student class there are two relationships: Teacher and School. In the School class I'm trying to find out all the students whose Teacher has a given id.

The problem is that I get

System.NullReferenceException: Object reference not set to an instance of an object.

in the statement

student.Teacher.Id

I thought of doing this.Students.Include("Teacher"), but this.Students doesn't have such a method. Any ideas how can I perform that query?

© Stack Overflow or respective owner

Related posts about entity-framework