LINQ 连接:选择一对多关系中的最后一条记录
LINQ join: selecting the last records in a one-to-many relationship
假设我有两个 table:
- Employees table:包含EmployeeId,DepartmentId...等字段
- Signings/Attendance table:包含SigningId,Time,EmployeeId,SignType(in or out)等字段...
我正在尝试 select 属于某个部门的每个员工在某个日期时间(通常 DateTime.Now)的最后一次签名,因为我正在为我的公司开发一个基于考勤的应用程序.
到目前为止,我有以下查询,其中 returns EmployeeId 和 SignType 用于满足前面条件(部门和时间)的所有员工。我知道这个查询应该有一个额外的连接(准确地说是左连接可能要归功于我昨天发现的 this post)但我真的不知道如何进行,因为这是我第一次这样做复杂的 linq 查询。
非常感谢。
from e in AppDbContext.Employees
join s in AppDbContext.Signings
on e.EmployeeId equals s.EmployeeId
where e.DepartmentId == DepartmentId
&& s.Time.Date == DateTime.Now.Date
select new Aux2()
{
Id = e.EmployeeId,
Type = s.SignType,
};
编辑:子查询尝试
AppDbContext.Employees
.Where(e => e.DepartmentId == DepartmentId )
.Select(e => new Aux2(){
Id = e.EmployeeId,
Type = AppDbContext.Signings
.Where(s => s.EmployeeId== e.EmployeeId && s.Time.Date==Date)
.OrderByDescending(s => s.Time)
.Take(1)
.Select(s => s.SigType)
.FirstOrDefault()
})
你可以这样做:
var maxDateSignsByEmployee = from sign in AppDbContext.Signings.Where(s => s.Time.Date == DateTime.Now.Date)
group sign by sign.EmployeeId into res
select new{
EmpId = res.Key,
MaxDate = resg.Max(x => x.Time)
};
var list = from e in AppDbContext.Employees
join s in AppDbContext.Signings.Where(s => maxDateSignsByEmployee.Where(m => m.EmpId == s.EmployeeId && m.MaxDate == s.Time).Count() > 0)
on e.WorkerId equals s.EmployeeId
where e.DepartmentId == DepartmentId
select new Aux2()
{
Id = e.EmployeeId,
Type = s.SignType,
};
应该是你需要的。这是一个基本的反应,你可以优化它。
假设我有两个 table:
- Employees table:包含EmployeeId,DepartmentId...等字段
- Signings/Attendance table:包含SigningId,Time,EmployeeId,SignType(in or out)等字段...
我正在尝试 select 属于某个部门的每个员工在某个日期时间(通常 DateTime.Now)的最后一次签名,因为我正在为我的公司开发一个基于考勤的应用程序.
到目前为止,我有以下查询,其中 returns EmployeeId 和 SignType 用于满足前面条件(部门和时间)的所有员工。我知道这个查询应该有一个额外的连接(准确地说是左连接可能要归功于我昨天发现的 this post)但我真的不知道如何进行,因为这是我第一次这样做复杂的 linq 查询。
非常感谢。
from e in AppDbContext.Employees
join s in AppDbContext.Signings
on e.EmployeeId equals s.EmployeeId
where e.DepartmentId == DepartmentId
&& s.Time.Date == DateTime.Now.Date
select new Aux2()
{
Id = e.EmployeeId,
Type = s.SignType,
};
编辑:子查询尝试
AppDbContext.Employees
.Where(e => e.DepartmentId == DepartmentId )
.Select(e => new Aux2(){
Id = e.EmployeeId,
Type = AppDbContext.Signings
.Where(s => s.EmployeeId== e.EmployeeId && s.Time.Date==Date)
.OrderByDescending(s => s.Time)
.Take(1)
.Select(s => s.SigType)
.FirstOrDefault()
})
你可以这样做:
var maxDateSignsByEmployee = from sign in AppDbContext.Signings.Where(s => s.Time.Date == DateTime.Now.Date)
group sign by sign.EmployeeId into res
select new{
EmpId = res.Key,
MaxDate = resg.Max(x => x.Time)
};
var list = from e in AppDbContext.Employees
join s in AppDbContext.Signings.Where(s => maxDateSignsByEmployee.Where(m => m.EmpId == s.EmployeeId && m.MaxDate == s.Time).Count() > 0)
on e.WorkerId equals s.EmployeeId
where e.DepartmentId == DepartmentId
select new Aux2()
{
Id = e.EmployeeId,
Type = s.SignType,
};
应该是你需要的。这是一个基本的反应,你可以优化它。