【问题标题】: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 =&gt; s.CourseId)吗?
  • 在 lambda 表达式中,s 代表 Student,而不是 StudentCourse。CourseID 不适用于 s。

标签: entity-framework-4.1 wcf-ria-services silverlight-5.0


【解决方案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 列表中的列必须是唯一的。”
猜你喜欢
  • 2010-11-15
  • 2021-04-04
  • 1970-01-01
  • 2018-11-28
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 2017-11-01
  • 2023-02-24
相关资源
最近更新 更多