2012-09-17 45 views
1

假設我在DB中有兩張表格:Student,StudentCourse。如何對Composite對象中的記錄進行排序?

在對學生的元數據,設置StudentCourse爲複合:

[Include] 
[Composition] 
public EntityCollection<StudentCourse> StudentCourses { get; set; } 

在爲學生域名服務,inlcude StudentCourse像:

public IQueryable<Student> GetStudentByID(int id) 
{ 
    reutrn this.ObjectContext.Students.Include("StudentCourse").Where(s => s.ID == id) as IQueryable<Student>; 
} 

的問題是:我想StudentCourse記錄可按照來自StudentCourses的列進行排序,例如,CourseID。

一名學生下的StudentCourse記錄實際上是StudentCourse的一個實體集合。

如何解決此問題?

+0

你有沒有嘗試添加'.OrderBy(S => s.CourseId)''之後。哪裏(...)'? –

+0

在lambda表達式中,s代表學生,而不是StudentCourse.CourseID不適用於s。 – KentZhou

回答

0

即使在普通的SQL中,你所要求的是不可能的。 您可以嘗試如果您的提供商將支持財產以後這樣的

this.ObjectContext.Students.Include("StudentCourse").Where(s => s.ID == id).OrderBy(x=> x.StudentCourse.First().CourseId) as IQueryable<Student> 
+0

謝謝。試過了。不工作。 – KentZhou

+0

所以我不相信一個乾淨的方式來做你想做的事情是可能的。此外,即使我上面提出的方式是......虛擬!它會通過他們的第一個StudentCourse.CourseID命令學生。它有多有用?我會重新制定一個更有意義的問題 – mCasamento

+0

謝謝。嘗試並得到奇怪的錯誤:「按列表順序中指定了多個列。按順序列表中的列必須是唯一的。」 – KentZhou

相關問題