如何将使用 group by 连接 2 个表转换为 linq

问题描述 投票:0回答:1

如何将此查询转换为 C# 中的 Linq?

SQL查询:

SELECT * 
FROM com.DepartmentPersonnel q1 
INNER JOIN 
    (SELECT dp.DepartmentID, MIN(dp.JobTypeID)JobTypeID  
     FROM com.DepartmentPersonnel dp
     GROUP BY dp.DepartmentID) q2 ON q1.DepartmentID = q2.DepartmentID 
                                  AND q1.JobTypeID = q2.JobTypeID
c# sql linq group-by
1个回答
0
投票

试试这个

var query = 
from q1 in dbContext.DepartmentPersonnel
join q2 in
    (from dp in dbContext.DepartmentPersonnel
     group dp by dp.DepartmentID into g
     select new { DepartmentID = g.Key, JobTypeID = g.Min(dp => dp.JobTypeID) })
on new { q1.DepartmentID, q1.JobTypeID } equals new { q2.DepartmentID, q2.JobTypeID }
select q1;
© www.soinside.com 2019 - 2024. All rights reserved.