所以,我一直在尝试将下面的SQL查询转换为实体框架查询。我一直在寻找使用group by的其他问题,但是当我遵循相同的语法时,我无法让我工作。这是SQL查询:
SELECT w.WorkstationID, WorkstationName, MinEmployeeCount,
OptimalEmployeeCount, MAX(Quantity) AS NumOfEmployees
FROM Workstation AS w
JOIN WorkstationRecord AS r
ON w.WorkstationID = r.WorkstationID
GROUP BY w.WorkstationID, WorkstationName, MinEmployeeCount,
OptimalEmployeeCount
目前,我尝试了几种不同的EF查询(在查询语法中):
Workstations = (from sta in context.Workstations
join rec in context.WorkstationRecords on sta.WorkstationID equals rec.WorkstationID
group sta by sta.WorkstationID into sta
select new ClientWorkstation()
{
WorkstationID = sta.WorkstationID,
WorkstationName = sta.WorkstationName,
MinEmployeeCount = sta.MinEmployeeCount,
OptimalEmployeeCount = sta.OptimalEmployeeCount,
NumOfEmployees = rec.Quantity,
}).ToList();
感谢任何帮助。
答案 0 :(得分:1)
改变这个:
group sta by sta.WorkstationID into sta
到
group sta by new { sta.WorkstationID, sta.WorkstationName, sta.MinEmployeeCount,
sta.OptimalEmployeeCount } into grp
select new ClientWorkstation
{
// Are you sure you want Max and not Count?
NumOfEmployees = grp.Max(t => t.Quantity),
// other members...
}