我有以下实体:
public class EmployeeCv : UserEntity
{
public byte ProfileImage { get; set; }
public virtual List<Header> Headers { get; set; }
[Index]
public virtual List<ProjectExperience> ProjectExperiences { get; set; }
[Index]
public virtual List<Tag> Tags { get; set; }
}
public class Tag : AuditableEntity<int>
{
public string Title { get; set; }
public virtual List<EmployeeCv> EmployeeCv { get; set; }
public virtual TagType TagType { get; set; }
}
public class TagType : AuditableEntity<int>
{
public string Title { get; set; }
public string CssClass { get; set; }
public virtual List<Tag> Tags { get; set; }
}
我创建的DTO类只继承了实际的EmployeeCv
和Employee
类,因为Entity Framework不允许我在select中使用相同的类。
public class EmployeeCvDTO : EmployeeCv
{
}
public class EmployeeDTO : Employee
{
}
我的LINQ查询:
var listOfEmployeeCV = from cvs in _context.EmployeeCvs.Include(x => x.Employee).Include(x => x.Tags).Include(x => x.Tags.Select(y => y.TagType))
.Include(x => x.ProjectExperiences)
where
_context.Employees.Any(
e => e.FirstName.Contains(SearchQuery.Keyword)
&& e.Id == cvs.Id
) || (_context.Tags.Any(t => t.Title.Contains(SearchQuery.Keyword) && cvs.Tags.Contains(t)))
|| (_context.ProjectExperiences.Any(pe => (pe.ProjectTitle.Contains(SearchQuery.Keyword)
|| pe.Description.Contains(SearchQuery.Keyword)) &&
cvs.ProjectExperiences.Contains(pe)))
select new
EmployeeCvDTO
{
Employee = new EmployeeDTO
{
FirstName = cvs.Employee.FirstName,
LastName = cvs.Employee.LastName,
EnterpriseId = cvs.Employee.EnterpriseId,
Level = cvs.Employee.Level
//ProfileImageData = x.Employee.ProfileImageData
},
Tags = cvs.Tags,
ProjectExperiences = cvs.ProjectExperiences,
};
上述查询完全符合我的要求。但是listOfEmployeeCV.Tags.TagType
对于所有标记都为空。即使我已将其包含在.include
语句中,但EmployeeCv -> Tags -> TagType
始终为null。
EmployeeCv
和Tags
具有多对多关系,而Tags
和TagType
具有一对多关系。
另外,添加以下LINQ也可以正常工作并返回tagType:
var lista = from cvs in _context.EmployeeCvs.Include(x => x.Employee).Include(x => x.Tags).Include(x => x.Tags.Select(y => y.TagType))
.Include(x => x.ProjectExperiences) select cvs;
var listb = lista.ToList();