我在LINQPad中键入了以下LINQ,并使用dbcontext连接到我的数据库(SQL Server 2005)。
from j in Jobs
where j.dValuationDate.HasValue && j.dValuationDate.Value > EntityFunctions.AddDays(DateTime.Now, -7).Value &&
j.dValuationDate.Value < EntityFunctions.AddHours(DateTime.Now, -1).Value &&
j.bXMLServiceProviderID.HasValue && j.bXMLServiceProviderID.Value == 1 &&
!ValuationDelays.Any(x => x.iJobID == j.iJobID && x.iStatusID == 5)
select j.iJobID
Jobs和ValuationDelays是我的数据库中由iJobId列链接的表。
LINQPad生成的SQL是:
SELECT
[Extent1].[iDelayID] AS [iDelayID],
[Extent1].[iJobID] AS [iJobID],
[Extent1].[sReasonforDelay] AS [sReasonforDelay],
[Extent1].[dDateTime] AS [dDateTime],
[Extent1].[iStaffID] AS [iStaffID],
[Extent1].[iStatusID] AS [iStatusID],
[Extent1].[dSentDate] AS [dSentDate],
[Extent1].[yValExDelayReasonID] AS [yValExDelayReasonID],
[Extent1].[dDelayedUntilDate] AS [dDelayedUntilDate],
[Extent1].[dValuationDate] AS [dValuationDate],
[Extent1].[valexActionUpdateId] AS [valexActionUpdateId],
[Extent1].[valexAppointmentReasonId] AS [valexAppointmentReasonId]
FROM [dbo].[ValuationDelays] AS [Extent1]
注意没有where子句。我无法理解为什么LINQPad会生成这样的查询。我做错了什么?
在VS 2010中调试时,查询运行正常,并在C#代码中返回预期结果。
答案 0 :(得分:3)
您已!ValuationDelays
取代!j.ValuationDelays
。
看起来这会导致EF从数据库加载所有ValuationDelay
并处理内存中的整个语句。我无法解释为什么Job
记录中没有查询谓词,但至少你现在有一些问题需要纠正。