I have the following model (dumbed down for this question):
public class Student
{
public int ID { get; set; }
public virtual ICollection<StudentCourse> StudentCourses { get; set; }
public Student(){
StudentCourses = new List<StudentCourse>();
}
}
public class StudentCourse
{
public int ID { get; set; }
public string Grade { get; set; }
public int StudentID { get; set; }
public int CourseID { get; set; }
public virtual Student Student { get; set; }
public virtual Course Course { get; set; }
}
public class Course
{
public int ID { get; set; }
public virtual ICollection<StudentCourse> StudentCourses { get; set; }
public Course() {
StudentCourses = new List<StudentCourse>();
}
}
Everything is linked in my DbContext class and working as expected. I want to find all the students that have taken part in a course so I do:
var query = db.Students
.Where(s => s.StudentCourses.Course.ID == 11);
but it fails because for some reason EF can’t see the link between StudentCourse and Course. The error message is:
‘System.Collections.Generic.ICollection’ does not contain a definition for ‘Course’ and no extension method ‘Course’ accepting a first argument of type ‘System.Collections.Generic.ICollection’ could be found (are you missing a using directive or an assembly reference?).
However the following works fine:
var query = db.StudentCourses
.Where(s => s.Course.ID == 11)
.Select(s=>s.Students);
Is this a bug or am I doing something wrong?
StudentCourses is a list of Courses, you can’t access a single course on it with .Course. In order to do that, you need to use a clause which works on individual records, such as select, where, any, etc. (most of the IEnumerable extensions taking a lambda expression do so for this purpose.)
If you want to work off of db.Students, fine, but the second example you posted is probably more semantically close to what you’re doing. StudentCourses represents the connection between students and courses. Your where clause restricts to a specific course, the select clause projects a student course collection over its connected student.