开发者

Problem with a Linq to Entity query that contains a passed list of entities

开发者 https://www.devze.com 2023-04-06 18:53 出处:网络
I have two entities User {UserID, Name, UserTypeID } StudentParent { StudentParentID, StudentID, ParentID }

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))});
0

精彩评论

暂无评论...
验证码 换一张
取 消