我正在尝试使用给定的UserID +链接到具有给定UserID的经理的所有工作来检索链接到员工的所有工作。我按照SQL UNION的方式加入数据集。这是我为此目的编写的代码,但似乎INCLUDE是累积的(第二个工作在第一个检索的数据集上),这不是我的意图(第二组数据必须添加到第一组) :
var list = await db.Employment.AsNoTracking()
.Where(x => x.Active)
.Include(x => x.Employee).Where(x => x.Employee.UserID == UserID)
.Include(x => x.Manager).Where(x => x.Manager.UserID == UserID)
.ToListAsync();
就业模式
public class Employment : IHasID, IValidatableObject
{
public int ID { get; set; }
public int EmployeeID { get; set; }
public Employee Employee { get; set; }
public bool Active { get; set; }
public int? ManagerID { get; set; }
public Employee Manager { get; set; }
}
员工模型
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? UserID { get; set; }
public User User { get; set; }
public bool Active { get; set; }
[InverseProperty("Employee")]
public List<Employment> EmploymentEmployee { get; set; }
[InverseProperty("Manager")]
public List<Employment> EmploymentManager { get; set; }
}
后来,我发现有一个Union扩展,所以我尝试使用它:
var list = await
db.Employment.Where(x => x.Active).Include(x => x.Employee)
.Include(x => x.Manager).Where(x => x.Employee.UserID == UserID)
.Union(
db.Employment.Where(x => x.Active).Include(x => x.Employee)
.Include(x => x.Manager).Where(x => x.Manager.UserID == UserID))
.ToListAsync();
...但是这会导致异常:InvalidOperationException:警告为警告'CoreEventId.IncludeIgnoredWarning'的错误异常:导航的Include操作:'x.Employee'被忽略,因为最终无法访问目标导航查询结果。