EF生成的SQL Query返回错误的结果

时间:2012-11-02 10:51:09

标签: sql entity-framework

我想过滤特定日期。由于我使用的控件,我也可以在一系列日期上进行过滤,但是当我在一个日期上过滤时,它也应该有效,所以我有足够的灵活性。

当我在2004-11-24过滤时,我的代码使用Entity Framework生成以下查询。但不知怎的,查询返回2004-11-25而不是2004-11-24的结果。

此查询有什么问题?

SELECT TOP (26) 
[Filter1].[Id] AS [Id], 
[Filter1].[AliquotId] AS [AliquotId], 
[Filter1].[ScientificStudyId] AS [ScientificStudyId], 
[Filter1].[ExpectedSpecimenId] AS [ExpectedSpecimenId], 
[Filter1].[ExpectedAliquotId] AS [ExpectedAliquotId], 
[Filter1].[VisitId] AS [VisitId], 
[Filter1].[BoxId] AS [BoxId], 
[Filter1].[ParticipantId] AS [ParticipantId], 
[Filter1].[Product] AS [Product], 
[Filter1].[BodyMaterial] AS [BodyMaterial], 
[Filter1].[AliquotNr] AS [AliquotNr], 
[Filter1].[ReservationName] AS [ReservationName], 
[Filter1].[Visit] AS [Visit], 
[Filter1].[Win32Color] AS [Win32Color], 
[Filter1].[Position] AS [Position], 
[Filter1].[Barcode] AS [Barcode], 
[Filter1].[Temperature] AS [Temperature], 
[Filter1].[DivergentStorageTemperature] AS [DivergentStorageTemperature], 
[Filter1].[BoxName] AS [BoxName], 
[Filter1].[CollectionDate] AS [CollectionDate], 
[Filter1].[Positioned] AS [Positioned], 
[Filter1].[CheckedOut] AS [CheckedOut], 
[Filter1].[CheckinDate] AS [CheckinDate], 
[Filter1].[CheckoutDate] AS [CheckoutDate], 
[Filter1].[LabelsPrinted] AS [LabelsPrinted], 
[Filter1].[ParticipantNr] AS [ParticipantNr], 
[Filter1].[Specimen] AS [Specimen], 
[Filter1].[ContainerType] AS [ContainerType], 
[Filter1].[ContainerTypeId] AS [ContainerTypeId], 
[Filter1].[Quantity] AS [Quantity], 
[Filter1].[Unit] AS [Unit], 
[Filter1].[QuantityUnit] AS [QuantityUnit], 
[Filter1].[StudyVersion] AS [StudyVersion], 
[Filter1].[ThawCount] AS [ThawCount], 
[Filter1].[Sop] AS [Sop], 
[Filter1].[NrOfRemarks] AS [NrOfRemarks], 
[Filter1].[RemarkSummary] AS [RemarkSummary]
FROM ( SELECT [Extent1].[Id] AS [Id], [Extent1].[AliquotId] AS [AliquotId], [Extent1]. [ScientificStudyId] AS [ScientificStudyId], [Extent1].[ExpectedSpecimenId] AS [ExpectedSpecimenId], [Extent1].[ExpectedAliquotId] AS [ExpectedAliquotId], [Extent1].[VisitId] AS [VisitId], [Extent1].[BoxId] AS [BoxId], [Extent1].[ParticipantId] AS [ParticipantId], [Extent1].[Product] AS [Product], [Extent1].[BodyMaterial] AS [BodyMaterial], [Extent1].[AliquotNr] AS [AliquotNr], [Extent1].[ReservationName] AS [ReservationName], [Extent1].[Visit] AS [Visit], [Extent1].[Win32Color] AS [Win32Color], [Extent1].[Position] AS [Position], [Extent1].[Barcode] AS [Barcode], [Extent1].[Temperature] AS [Temperature], [Extent1].[DivergentStorageTemperature] AS [DivergentStorageTemperature], [Extent1].[BoxName] AS [BoxName], [Extent1].[CollectionDate] AS [CollectionDate], [Extent1].[Positioned] AS [Positioned], [Extent1].[CheckedOut] AS [CheckedOut], [Extent1].[CheckinDate] AS [CheckinDate], [Extent1].[CheckoutDate] AS [CheckoutDate], [Extent1].[LabelsPrinted] AS [LabelsPrinted], [Extent1].[ParticipantNr] AS [ParticipantNr], [Extent1].[Specimen] AS [Specimen], [Extent1].[ContainerType] AS [ContainerType], [Extent1].[ContainerTypeId] AS [ContainerTypeId], [Extent1].[Quantity] AS [Quantity], [Extent1].[Unit] AS [Unit], [Extent1].[QuantityUnit] AS [QuantityUnit], [Extent1].[StudyVersion] AS [StudyVersion], [Extent1].[ThawCount] AS [ThawCount], [Extent1].[Sop] AS [Sop], [Extent1].[NrOfRemarks] AS [NrOfRemarks], [Extent1].[RemarkSummary] AS [RemarkSummary], row_number() OVER (ORDER BY [Extent1].[CollectionDate] ASC) AS [row_number]
FROM [dbo].[ClaimedAliquotPositionsReadModels] AS [Extent1]
WHERE (cast('0ebebae6-594d-4be0-ac8b-a078013f1370' as uniqueidentifier) = [Extent1].[ScientificStudyId])
AND ([Extent1].[CheckoutDate] >= convert(datetime2, '2004-11-24 00:00:00.0000000', 121))
AND ([Extent1].[CheckoutDate] < convert(datetime2, '2004-11-25 00:00:00.0000000', 121))
)  AS [Filter1]
WHERE [Filter1].[row_number] > 0
ORDER BY [Filter1].[CollectionDate] ASC

1 个答案:

答案 0 :(得分:2)

通过解析我的输入解决了这个问题。

DateTime.TryParse(splitted[0], CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal | DateTimeStyles.AdjustToUniversal, out date1)

代替

DateTime.TryParse(splitted[0], out date1)

这导致以下where子句

WHERE (cast('0ebebae6-594d-4be0-ac8b-a078013f1370' as uniqueidentifier) = [Extent1].[ScientificStudyId])
AND ([Extent1].[CheckoutDate] >= convert(datetime2, '2004-11-23 23:00:00.0000000', 121))
AND ([Extent1].[CheckoutDate] < convert(datetime2, '2004-11-24 23:00:00.0000000', 121))

生成的sql是提供值的前一天。因此2004-11-24变为2004-11-23