如何对复合对象中的记录进行排序?
Posted
技术标签:
【中文标题】如何对复合对象中的记录进行排序?【英文标题】:How to sort records in Composite object? 【发布时间】:2012-09-17 13:20:23 【问题描述】:假设我在 DB 中有 2 个表:Student、StudentCourse。
在 Student 的元数据中,将 StudentCourse 设置为 Composite:
[Include]
[Composition]
public EntityCollection<StudentCourse> StudentCourses get; set;
在 Student 的域服务中,包括 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的一个实体集合。
如何解决这个问题?
【问题讨论】:
你试过在.Where(...)
之后添加.OrderBy(s => s.CourseId)
吗?
在 lambda 表达式中,s 代表 Student,而不是 StudentCourse。CourseID 不适用于 s。
【参考方案1】:
即使在普通的 SQL 中,您所要求的也是不可能的。 你可以试试你的供应商是否支持这样的东西
this.ObjectContext.Students.Include("StudentCourse").Where(s => s.ID == id).OrderBy(x=> x.StudentCourse.First().CourseId) as IQueryable<Student>
【讨论】:
所以我不相信有一种干净的方式来做你想做的事。此外,即使我在上面建议的方式也是......假的!它将按学生的第一个 StudentCourse.CourseID 排序。它有多大用处?我会将问题重新表述为更有意义的问题 谢谢。尝试并得到奇怪的错误:“在 order by 列表中多次指定了一个列。order by 列表中的列必须是唯一的。”以上是关于如何对复合对象中的记录进行排序?的主要内容,如果未能解决你的问题,请参考以下文章
MongoDB:如何对 MySQL 字段关键字等结果中的记录进行排序 [重复]