我是LINQ的新手,我做过简单的查询,但是我在这个问题上遇到了障碍。有人可以帮我转换成LINQ吗?把桌子连接到自己和记录的计数让我感到很高兴。
SELECT DISTINCT
RegionID = O.Region,
Region = R.Office,
OfficeCount = (SELECT
COUNT(officeID)
FROM OFfice
WHERE Region = O.region
AND Type IN (5, 6)
AND ClosedProduction = 0
AND OfficeID NOT IN (10, 135, 151)
AND (OfficeID IN (SELECT DISTINCT
OfficeID
FROM WR_Data_Work
WHERE PhaseID IS NOT NULL)
OR OfficeID = 154))
FROM office O
JOIN Office R
ON O.Region = R.OfficeID
JOIN Employee VP
ON R.VicePresID = VP.EmployeeID
WHERE O.OfficeID NOT IN (10, 135, 151)
AND O.Type IN (5, 6)
AND O.ClosedProduction = 0
AND (O.OfficeID IN (SELECT DISTINCT
OfficeID
FROM WR_Data_Work
WHERE PhaseID IS NOT NULL)
OR O.OfficeID = 154)
ORDER BY RegionID
OfficeID OfficeName Region 88 Office 1 90 90 Office 2 90 96 Office 3 90 86 Office 4 93 91 Office 5 93 92 Office 6 93 93 Office 7 93 95 Office 8 93
在这种情况下,Office 2是办公室1和3的区域办事处,但办公室7是4,5,6和8的区域办事处。
答案 0 :(得分:0)
这是我的尝试,没有任何测试能力:
var PhaseOffices = (from w in WR_Data_Work where w.PhaseID != null select w.OfficeID).Distinct();
var TargetOffices = from O in Office
where (O.Type == 5 || O.Type == 6) && O.ClosedProduction == 0 &&
(!(new[] { 10, 135, 151 }).Contains(O.OfficeID)) && (PhaseOffices.Contains(O.OfficeID) || O.OfficeID == 154)
select O;
var res = (from O in TargetOffices
join R in Office on O.Region equals R.OfficeID
//join VP in Employee on R.VicePresID equals VP.EmployeeID = not needed?
orderby O.Region
select new {
RegionID = O.Region,
Region = R.Office,
OfficeCount = (from ofc in TargetOffices
where ofc.Region == O.Region
select ofc.OfficeID).Count()
}).Distinct();
某些LINQ提供程序无法处理我使用的本地数组Contains
,在这种情况下,您必须将其替换为与Type
一样的单独测试。