2011-09-22 97 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學生名單中獲得父母。 –