I am working with Entity Framework code-first, and I have a class Course
which has a navigation property Students
:
public virtual Collection<Student> Students { get; set;}
It works ok, but as I access this navigation property, all the data is retrieved from the database:
var allStudents = course.Students; // Here it retrieves the data
var activeStudents = allStudents.Where(n => n.Active); // Here it filter the data on memory
var listOfActiveStudents = activeStudents.ToList(); // It already has the data on memory.
As you can imagine, I need the query to be executed when I do the .ToList()
because I don't want to bring all the Students
from the database, only the active ones.
Do you know what I am doing wrong?
See Question&Answers more detail:os