2011-03-04 72 views
0

我有兩個表,「學生」表和「分數」表與外鍵。我還定義了一個自定義類,如下所示:LINQ - 如何使用子查詢查詢單個實體來填充多個實體?

public class Student 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string EmailAddress { get; set; } 
    public List<int> Scores { get; set; } 
} 

我該如何去寫一個LINQ查詢來檢索EmailAddress的一個學生的所有成績?我到目前爲止...

(from student in myContext.Students 
where student.EmailAddress == emailAddress 
select new Student() 
{ 
    FirstName = student.FirstName, 
    LastName = student.LastName, 
    Scores = ??? 
}).SingleOrDefault(); 
+0

分數是否可以變爲所有分數或所有分數的單子? – 2011-03-04 16:02:38

+0

該個別學生的分數列表。分數表有一個「StudentID」外鍵。 – 2011-03-04 16:14:52

回答

1

不知道你的ef模型看起來很難具體,但我想這樣的事情應該工作。

(from student in myContext.Students 
where student.EmailAddress == emailAddress 
select new Student() 
{ 
    FirstName = student.FirstName, 
    LastName = student.LastName, 
    Scores = student.Scores.Select(studentScore=>studentScore.value) 
}).SingleOrDefault();