2011-09-22 72 views
1

的传递的列表中,我有两个实体问题与LINQ到实体查询包含实体

User { UserID, Name, UserTypeID } 
StudentParent { StudentParentID, StudentID, ParentID } 

// where 
UserTypeID { 1=student, 2=parent } 

两个StudentParent.StudentIDStudentParent.ParentID是指向User.UserID

我的学生名单外键(IEnumerable<User> ),我需要得到父母的名单。我需要帮助搞清楚正确的表达方式来获得父母的名单。

我是否应该使用包含或任何语句来匹配学生用户列表?

+1

这需要清理。 当你说你有一个学生IEnumerable的列表,是所有类型的用户列表中的对象?它是IEnumerable ? –

+0

我正在创建一个函数,我正在传递一个名为students的IEnumerable 列表。在这个函数中,我试图让我传递的所有学生的父母(也是User类型的)。​​ –

回答

0

你应该能够SelectParent来自你的IEnumerable的学生。

students.SelectMany(s => s.StudentParents.Select(sp => sp.ntParent_Parent)); 

此执行从学生的集合,不是所有Students的投影。

按理正是在这里像

  • 是一组学生的
  • 为每个学生,让学生家长实体
  • 为每个studentparent实体,获取父

选择匿名类型可能更有用,因此您可以查看哪些父母属于哪些学生。

students.Select(s => new { 
    Student = s, 
    Parents = s.StudentParents.Select(sp => sp.ntParent_Parent))}); 
+0

我在用户实体中的导航属性是StudentParent_Parent和StudentParent_Student。但我也只需要从我通过的IEnumerable学生名单中获得父母。 –