How can I filter for specific information from an EntityCollection?
I'd like to display grade information for each student in the results
variable.
The way my database is set up is:
Grade --- GradeStudent --- Student
So each different year, I have a unique GradeStudent
record holding the grade's ID, and the students ID, as well as the year of that record.
I used Entity Framework 4 as my ORM, and I'd like to display the grade name in the dataGridView for each student.
private void btnSearch_Click(object sender, EventArgs e)
{
StudentRepository repo = new StudentRepository();
var results =
repo.FindAllStudents().Where(
s =>
s.Name == txtSearchQuery.Text ||
s.LastNameFather == txtSearchQuery.Text ||
s.LastNameMother == txtSearchQuery.Text);
dataGridView1.DataSource = results.Select(s => new
{
Codigo = s.StudentId,
Nombre = s.LastNameFather + " " + s.L开发者_开发问答astNameMother + ", " + s.Name,
Sexo = s.Sex,
Telefono = s.Telephone
}).ToList();
}
If I try to do the following, I can only invoke an EntityCollection collection, since there can be many GradeStudent objects associated with my student. One for each year.
So I would need to get the grade he was in during X year.
Do you have any suggestions on how to accomplish this?
If I understand the question - you are looking to get the Grade object (via the GradeStudent middle table). If this is the case, you would need to create a NavigationProperty in your EF model to relate Grade to GradeStudent(* - 1) and GradeStudent to Student (0..1 - *).
As for fetching the data into your context, is this a silverlight app using RIA? If so, the GetStudentsQuery method in your service would need to include those entities so that data is returned with your Student object.
If grade
is known, this should work:
StudentRepository repo = new StudentRepository();
var results =
repo.FindAllStudents().Where(
s =>
(s.Name == txtSearchQuery.Text ||
s.LastNameFather == txtSearchQuery.Text ||
s.LastNameMother == txtSearchQuery.Text) &&
s.GradeStudent.Any(gs => gs.Grade = grade));
精彩评论