【发布时间】:2015-01-25 17:12:27
【问题描述】:
我在过滤子集合时遇到问题,过滤已完成,但我需要在集合中包含额外的实体,我不知道该怎么做
我有一个实体 Employee
public class Employee
{
[Key]
public int EmployeeId { get; set; }
public string EmpNum { get; set; }
public virtual ICollection<EmployeeProfile> EmployeeProfiles { get; set; }
}
和集合实体EmployeeProfile
public class EmployeeProfile
{
[Key]
public int EmployeeProfileId { get; set; }
[ForeignKey("Employee")]
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
[ForeignKey("Nationality")]
public int? NationalityId{ get; set; }
....
public DateTime? UpdateDatetime { get; set; }
public virtual Nationality Nationality{ get; set; }
public virtual Employee Employee { get; set; }
}
我用投影来过滤
var employees = context.Employees
.Include(e => e.EmployeeProfiles)
.Include(e => e.EmployeeProfiles.Select(a => a.Nationality))
.Select(e => new EmployeeLeatestProfileModel
{
EmployeeId = e.EmployeeId
,
EmpNum = e.EmpNum
,
LastUpdatedProfile = e.EmployeeProfiles
.Where(p => p.EmployeeId == e.EmployeeId)
.OrderByDescending(a => a.UpdateDatetime).Take(1)
});
这是结果模型
public class EmployeeLeatestProfileModel {
public int EmployeeId { get; set; }
public string EmpNum { get; set; }
public IEnumerable<EmployeeProfile> LastUpdatedProfile;
}
问题是当我尝试获取国籍时,我没有找到它的数据,这似乎不包含在查询中!!
那么我可以做些什么来为子集合 EmoployeeProfile 包含国籍实体或任何其他实体
顺便说一句,我没有使用延迟加载
【问题讨论】:
标签: c# linq entity-framework asp.net-mvc-4