I have two entities
User { UserID, Name, UserTypeID }
StudentParent { StudentParentID, StudentID, ParentID }
// where
UserTypeID { 1=student, 2=parent }
Both StudentParent.StudentID
and StudentParent.ParentID
are foreign k开发者_运维技巧eys pointing to User.UserID
I have a list of Students (IEnumerable<User>
) that I need to get the list of parents for. I need help figuring out the proper expression to get the list of parents.
Should I be using a contains or any statement to match against the list of Student Users?
You should be able to Select
the Parent
entites from your IEnumerable
of students.
students.SelectMany(s => s.StudentParents.Select(sp => sp.ntParent_Parent));
This performs a projection from your collection of students, not all Students
.
Logically it is something like
- here's a set of students
- for each of those students, get the student parent entities
- for each of those studentparent entities, get the parent
It also may be more useful to select into an anonymous type so you can see which parents belong to which students.
students.Select(s => new {
Student = s,
Parents = s.StudentParents.Select(sp => sp.ntParent_Parent))});
精彩评论