简体   繁体   中英

Include Entities to Child Collection With Filter Linq EF5

I have a problem while filtering a child collection, the filtration is done but i need to include extra entities to the collection, i don't know how to do that

I have an entity Employee

public class Employee
{
    [Key]
    public int EmployeeId { get; set; }

    public string EmpNum { get; set; }

    public virtual ICollection<EmployeeProfile> EmployeeProfiles { get; set; }
}

and the collection entity 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; }
}

i used projection to filter

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)
            });

and this is the result model

public class EmployeeLeatestProfileModel {


    public int EmployeeId { get; set; }
    public string EmpNum { get; set; }
    public IEnumerable<EmployeeProfile> LastUpdatedProfile;

}

the thing is when i am trying to fetch the Nationality i didn't find the data of it which seems like it is not included in the query!!

so what can i do to include the Nationality entity or aany other entities for the child collection EmoployeeProfile

and i am not using Lazy Loading by the way

Instead of using projection you could use the object query include passing in a string.

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

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

Haven't tested this but try selecting into an anonymous collection.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM