我正在使用 EF core 7 和 C#
在模型中,一名员工有多个期间和许多票证。
我有以下课程:
public class Employee
{
public string EmployeeId { get; set; }
public string FullName { get; set; }
public List<Period> Periods { get; set; }
public List<Ticket> Tickets { get; set; }
}
public class Period
{
public int PeriodId { get; set; }
public DateTime From { get; set; }
public DateTime To { get; set; }
public Employee Employee { get; set; }
public string EmployeeId { get; set; } //FK pointing to Employee
}
public class Ticket
{
public string TicketId { get; set; }
public DateTime CreationDate { get; set; }
public string EmployeeId { get; set; } //FK pointing to Employee
}
如何编写一个 linq 查询来返回其中的票证 员工的期限范围?,我需要的结果为
IQueryable<Ticket>
。
这是我正在尝试的查询:
//I create a list of employees Id's
var employeeIds= new List<string> {"9854","4587","2587"}
IQueryable<Ticket> result =
from ticket in DbContext.Tickets
where employeeIds.Contains(ticket.EmployeeId) &&
//Here I need to set the condition for tickets
// that are inside the Period of each employee
//[Period.From , Period.To]
select ticket;
这应该就是你想要的:
IQueryable<Ticket> result =
from ticket in Tickets
join employee in Employee on ticket.EmployeeId equals employee.EmployeeId
where employeeIds.Contains(employee.EmployeeId)
&& Periods.Any(p => p.EmployeeId == employee.EmployeeId
&& ticket.CreationDate >= p.From && ticket.CreationDate <= p.To)
select ticket;