【发布时间】:2017-10-27 09:20:47
【问题描述】:
我有两张桌子:
db.EmployeeReason
db.employee
具有以下字段:
CREATE TABLE [dbo].[Employee_Reason](
[Id] [int] IDENTITY(1,1) NOT NULL,
[IdEmployee] [int] NOT NULL,
[IdReason] [int] NOT NULL,
[AverageServiceTime] [time](0) NULL,
[Skillset] [bit] NOT NULL,
CONSTRAINT [PK_Employee_Reason] PRIMARY KEY CLUSTERED ([Id] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Employee_Reason] WITH CHECK ADD CONSTRAINT [FK_Employee_Reason_Employee] FOREIGN KEY([IdEmployee])
REFERENCES [dbo].[Employee] ([Id])
GO
员工表如下所示:
CREATE TABLE [dbo].[Employee](
[Id] [int] IDENTITY(1,1) NOT NULL,
[EmployeeId] [int] NOT NULL,
[FirstName] [nvarchar](20) NOT NULL,
[LastName] [nvarchar](40) NOT NULL,
[IdLocation] [int] NOT NULL,
[IdStatus] [int] NOT NULL,
[UserId] [nvarchar](450) NOT NULL,
[IsArchived] [bit] NULL,
[IdAssignedLocation] [int] NOT NULL,
[IdQueue] [int] NOT NULL,
[IdPosition] [int] NOT NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED ([Id] ASC) ON [PRIMARY]
) ON [PRIMARY]
我想将employee_reason 与idreason 分组,然后将join 与使用Employee_Reason.idemployee 和employee.id 的员工分组,如下所示:
List<SelfServeReason> skillsetReasons =
from reason in db.EmployeeReason
group reason by IdReason into reasonGroup
join employee in scheduleEmployees on reasonGroup.IdEmployee equals employee.Id
select new skillsetReason
{
id = reasonGroup.Id,
description = reasonGroup.Description,
estimatedWaitTime = min(employee.NextAvailableTime) – ConvertToCST(DateTime.Now)
};
我是这样写的:
select MAX(er.Id)
from Employee_Reason er join Employee e on er.IdEmployee = e.id
group by er.IdReason
但不确定如何将employee_reasons 分组,然后将join 与idempoyee 分组。
linq 不正确,它只是伪代码,如果有人可以建议什么是正确的 linq,那也很有帮助。
您能否提出解决方案?
【问题讨论】:
-
您指定了要分组的列,但没有指定要选择的列。与该 linq 查询中的相同吗?
-
@GabrielRainha 是 linq 中的相同
-
同样 linq 不正确,它只是伪代码,如果有人可以建议 wha 将是正确的 linq 也很有帮助。
-
但是 Employee 表上没有 NextAvailableTime 列,而您正在 linq 上查询该列。
标签: sql-server linq join group-by