2012-09-17 43 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

相关问题