I’m trying out Linq for the first time and having a bit of difficult retrieving the child objects of an entity. I have a course table which has a one to many relationship with the department table (ie. one department can have one or many courses).
When I select the a particular department I want to bind the courses relating to that particular department to a gridview however the coursecount variable always returns a zero even when there are many courses related to that particular department. The dept object seems to be populated correctly apart from the courses objects.
Am I missing something obvious?
int deptid = Convert.ToInt32(cboDepartmentList.SelectedValue); Department dept = schoolcontext.Department.First(p=> p.DepartmentID == deptid); int coursecourse = dept.Course.Count; gvCourse.DataSource = dept.Course; gvCourse.AutoGenerateColumns = true; gvCourse.DataBind();
schoolcontext.Department.Include(‘Course’).First…
EDIT: the above is the quick answer… more details are here if you want them.