[英]Order by Collection entity property in ASP.NET MVC 3 (entity framework)
I have two entities like: 我有两个实体,如:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Lastname { get; set; }
public virtual ICollection<EmployeeEducation> EducationList { get; set; }
}
and 和
public class EmployeeEducation
{
public int Id { get; set; }
public int EmployeeId { get; set; }
public int Type { get; set; }
[ForeignKey("EmployeeId")]
public virtual Employee Employee { get; set; }
}
My question is, how can I get a specific employee and this employee's education list ordered by Type property? 我的问题是,如何获得特定员工和此类员工的教育列表按类型属性排序?
I have tried: 我努力了:
Employee employee = _work.EmployeeRepository.GetSet()
.SelectMany(e => e.EducationList, (e,d) => new { e, d })
.OrderBy(x => x.d.Type)
.Select(x => x.e)
.FirstOrDefault(e => e.Id == id);
But it does't seem to be sorting. 但它似乎并没有排序。 What is the correct way to do this?
这样做的正确方法是什么?
Thanks for everyone... 谢谢大家......
Depending if u are using POCO or not u should either use CreateSourceQuery() or Query() In the case of POCO something like: 取决于你是否使用POCO你应该使用CreateSourceQuery()或Query()在POCO的情况下,例如:
Employee employee = _work.EmployeeRepository.GetSet()
.SelectMany(e => e.EducationList, (e,d) => new { e, d })
.Query()
.OrderBy(x => x.d.Type)
.Select(x => x.e)
.FirstOrDefault(e => e.Id == id);
You do SelectMany()
, but never use the produced EducationList
part, becuase you do .Select(x => xe)
. 你做
SelectMany()
,但从不使用生成的EducationList
部分,因为你做。选择.Select(x => xe)
。 But couldn't life be simpler? 但生活难道不简单吗? After all, you only get 1 employee, why not sort its
EducationList
as soon as you need it, after having Include
d it, if necessary: 毕竟,你只有1名员工,为什么不在需要之后立即对其
EducationList
排序,如果有必要,请在Include
它之后:
Employee employee = _work.EmployeeRepository.GetSet().Include("EducationList")
.FirstOrDefault(e => e.Id == id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.