我尝试将SQL查询转换为LINQ,我不知道为什么相同的查询(对我而言)返回不同的结果。有人可能知道出了什么问题?这些查询之间有什么区别?
-- Region Parameters
DECLARE @p0 DateTime = '2017-08-10 00:00:00.000'
DECLARE @p1 DateTime = '2017-08-11 00:00:00.000'
DECLARE @p2 Int = 100
DECLARE @p3 NVarChar(1000) = 'N02'
DECLARE @p4 NVarChar(1000) = 'VVV'
-- EndRegion
SELECT COUNT(*) AS [Count], [t6].[idComm_Employee] AS [IdEmployee]
FROM (
SELECT [t4].[idComm_Employee]
FROM [tabComm_Item] AS [t0]
INNER JOIN [tabMES_Order] AS [t1] ON [t0].[idComm_Item] = [t1].[idComm_Item]
INNER JOIN [tabMES_Order_Line] AS [t2] ON ([t1].[idMES_Order] = [t2].[idMES_Order]) AND ([t1].[idMES_Order_Sequence] = [t2].[idMES_Order_Sequence])
INNER JOIN [tabHIST_Employee_Orders] AS [t3] ON ([t2].[idMES_Order] = [t3].[idMES_Order]) AND ([t2].[idMES_Order_Sequence] = [t3].[idMES_Order_Sequence])
INNER JOIN [tabHIST_Employee] AS [t4] ON ([t3].[idComm_Employee] = [t4].[idComm_Employee]) AND ([t3].[idHIST_WorkPlace] = [t4].[idHIST_WorkPlace])
INNER JOIN [tabHIST_WorkPlace] AS [t5] ON [t4].[idHIST_WorkPlace] = [t5].[idHIST_WorkPlace]
WHERE ([t5].[DateTimeFrom] > @p0) AND ([t5].[DateTimeTo] < @p1) AND ([t1].[idMES_Order_Typ] = @p2) AND ([t2].[idERP_Workcenter] <> @p3) AND ([t2].[idERP_Workcenter] <> @p4)
GROUP BY [t4].[idComm_Employee], [t0].[MachineTyp]
) AS [t6]
GROUP BY [t6].[idComm_Employee]
和我的
SELECT RESULT.idComm_Employee, COUNT(RESULT.MachineTyp)
FROM
(SELECT THE.idComm_Employee, TCI.MachineTyp
FROM
tabComm_Item AS TCI
INNER JOIN tabMES_Order AS TMO
ON TCI.idComm_Item = TMO.idComm_Item
INNER JOIN tabMES_Order_Line AS TMOL
ON TMO.idMES_Order = TMOL.idMES_Order AND TMO.idMES_Order_Sequence = TMOL.idMES_Order_Sequence
INNER JOIN tabHIST_Employee_Orders AS THEO
ON TMOL.idMES_Order = THEO.idMES_Order AND TMOL.idMES_Order_Sequence = THEO.idMES_Order_Sequence AND TMOL.idMES_Order_Line = THEO.idMES_Order_Line
INNER JOIN tabHIST_Employee AS THE
ON THEO.idComm_Employee = THE.idComm_Employee AND THEO.idHIST_WorkPlace = THE.idHIST_WorkPlace
INNER JOIN tabHIST_WorkPlace AS THWP
ON THE.idHIST_WorkPlace = THWP.idHIST_WorkPlace
WHERE
THWP.DateTimeFrom > '2017-08-10' AND THWP.DateTimeTo < '2017-08-11' AND TMO.idMES_Order_Typ = 100 AND TMOL.idERP_Workcenter <> 'N02' AND TMOL.idERP_Workcenter <> 'VVV'
GROUP BY THE.idComm_Employee, TCI.MachineTyp) AS RESULT
GROUP BY RESULT.idComm_Employee
我的查询返回122行,linq 130.在linq查询此表达式:
AND ([t2].[idERP_Workcenter] <> @p3) AND ([t2].[idERP_Workcenter] <> @p4)
被忽略
答案 0 :(得分:1)
在没有看到示例数据和表结构的情况下很难确定,但是在第二个查询中,您使用其他谓词加入了tabHIST_Employee_Orders表:
AND TMOL.idMES_Order_Line = THEO.idMES_Order_Line
第一个查询中不存在该谓词,这可能会导致两个查询之间的结果出现差异。