【问题标题】:Include Entities to Child Collection With Filter Linq EF5使用过滤器 Linq EF5 将实体包含到子集合中
【发布时间】: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


    【解决方案1】:

    您可以使用对象查询来代替投影,包括传入一个字符串。

    https://msdn.microsoft.com/en-us/library/bb738708(v=vs.110).aspx

    .Include(e => e.EmployeeProfiles)
    .Include("EmployeeProfiles.Nationality")
    .Include("EmployeeProfiles.Employee")
    

    尚未对此进行测试,但请尝试选择匿名集合。

    var employees = context.Employees
        .Select(e => new
        {
            Employee = e,
            EmployeeProfiles = e.EmployeeProfiles,
            Nationalities = e.EmployeeProfiles.Select(a => a.Nationality)
        })
        .ToList() // do this to force the projection
        .Select(e => new EmployeeLeatestProfileModel
        {
            EmployeeId = e.Employee.EmployeeId,
            EmpNum = e.Employee.EmpNum,
            LastUpdatedProfile = e.EmployeeProfiles
                .Where(p => p.EmployeeId == e.Employee.EmployeeId)
                .OrderByDescending(a => a.UpdateDatetime)
                .Take(1)
        })
        .ToList();
    

    【讨论】:

    • 我试过了,但同样的问题,没有投影值即将到来,但我必须使用 select 来过滤子集合,还是有其他方法?
    • 添加了另一种投影方法。没有测试所以不确定它是否有效。
    • 非常感谢 daivd 先生,但不幸的是它给了我例外
    • 我可以选择使用多重连接,但我在想是否有更好的方法
    • 有哪些例外?我看到您的 EmployeeLeatestProfileModel 类有一个可枚举的 LastUpdatedProfile,但是您正在执行 Take(1)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 2019-07-10
    相关资源
    最近更新 更多