FirstOrDefault 在比较 DateTime 时在不同的计算机上给出不同的结果?对象
FirstOrDefault giving different results on different computers when comparing DateTime? objects
我正在尝试比较 IQueryable 中的两个可为 null 的 DateTime 对象(从数据库中获取),如下所示:
result.FirstOrDefault(x => x.FirstDateTime == secondDateTime)
这在某些开发人员的计算机上可以正确执行,但在我的计算机上却不能。它没有给出任何结果。因为如果我将结果转换为这样的列表,它会起作用:
result.ToList().FirstOrDefault(x => x.FirstDateTime == secondDateTime)
当然我不想留下那样的代码,只是为了表明这不是因为在 IQueryable 中找不到日期
两个值都是 UTC。我尝试使用 Single
、SingleOrDefault
、First
和 Where
,结果都相同。我也试过比较 FirstDateTime.Value
和 secondDateTime.Value
。同样,结果相同
IQueryable 的提供程序是
System.Data.Entity.Internal.Linq.DbQueryProvider.
这可能是什么原因造成的?为什么第一段在某些计算机上可以工作,但在其他计算机上却不能,这取决于什么?
这是正在生成的 SQL :
FROM ( SELECT
[Extent1].[Id] AS [Id],
[Extent1].[OtherProperty1] AS [OtherProperty1],
[Extent1].[Version] AS [Version],
[Extent1].[Submitted] AS [Submitted],
[Extent1].[SubmittedBy] AS [SubmittedBy],
[Extent1].[Created] AS [Created],
[Extent1].[CreatedBy] AS [CreatedBy],
[Extent2].[Value] AS [Value],
[Extent2].[OtherProperty2] AS [OtherProperty2],
[Extent2].[Description] AS [Description],
[Extent2].[Latitude] AS [Latitude],
[Extent2].[Longitude] AS [Longitude],
[Extent2].[OtherProperty3] AS [OtherProperty3],
[Extent2].[OtherProperty4] AS [OtherProperty4],
[Extent2].[FirstDateTime] AS [FirstDateTime],
''0X0X'' AS [C1]
FROM [dbo].[StructuredInformations] AS [Extent1]
INNER JOIN [dbo].[Positions] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id]
WHERE ([Extent1].[OtherProperty1] = @p__linq__0) AND ([Extent2].
[FirstDateTime] IS NOT NULL) AND ([Extent2].[FirstDateTime] = @p__linq__1)
) AS [Project1]
ORDER BY [Project1].[OtherProperty2] DESC, [Project1].[FirstDateTime] ASC,
[Project1].[Value] ASC',N'@p__linq__0 int,@p__linq__1
datetime2(7)',@p__linq__0=7,@p__linq__1='2016-10-11 11:45:53.6230000'
IQueryable
不保证顺序。 List<T>
确实如此。要保持 IQueryable
并保持顺序,您应该尝试对 IQueryable
进行排序,而不是尝试获得第一个结果:
result.OrderBy(y => y.FirstDateTime).FirstOrDefault(x => x.FirstDateTime == secondDateTime)
问题类似于this - looks like the LINQ provider generated a datetime value with a fractional second precision of 7 (e.g. 2016-10-11 11:45:53.6230000) while the column in SQL is defined as datetime, which has a precision lower than 7, as opposed to datetime2,可以达到7的精度
要解决此问题,您可以将 SQL 中的列类型从 datetime 更改为 datetime(7)
我正在尝试比较 IQueryable 中的两个可为 null 的 DateTime 对象(从数据库中获取),如下所示:
result.FirstOrDefault(x => x.FirstDateTime == secondDateTime)
这在某些开发人员的计算机上可以正确执行,但在我的计算机上却不能。它没有给出任何结果。因为如果我将结果转换为这样的列表,它会起作用:
result.ToList().FirstOrDefault(x => x.FirstDateTime == secondDateTime)
当然我不想留下那样的代码,只是为了表明这不是因为在 IQueryable 中找不到日期
两个值都是 UTC。我尝试使用 Single
、SingleOrDefault
、First
和 Where
,结果都相同。我也试过比较 FirstDateTime.Value
和 secondDateTime.Value
。同样,结果相同
IQueryable 的提供程序是
System.Data.Entity.Internal.Linq.DbQueryProvider.
这可能是什么原因造成的?为什么第一段在某些计算机上可以工作,但在其他计算机上却不能,这取决于什么?
这是正在生成的 SQL :
FROM ( SELECT
[Extent1].[Id] AS [Id],
[Extent1].[OtherProperty1] AS [OtherProperty1],
[Extent1].[Version] AS [Version],
[Extent1].[Submitted] AS [Submitted],
[Extent1].[SubmittedBy] AS [SubmittedBy],
[Extent1].[Created] AS [Created],
[Extent1].[CreatedBy] AS [CreatedBy],
[Extent2].[Value] AS [Value],
[Extent2].[OtherProperty2] AS [OtherProperty2],
[Extent2].[Description] AS [Description],
[Extent2].[Latitude] AS [Latitude],
[Extent2].[Longitude] AS [Longitude],
[Extent2].[OtherProperty3] AS [OtherProperty3],
[Extent2].[OtherProperty4] AS [OtherProperty4],
[Extent2].[FirstDateTime] AS [FirstDateTime],
''0X0X'' AS [C1]
FROM [dbo].[StructuredInformations] AS [Extent1]
INNER JOIN [dbo].[Positions] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id]
WHERE ([Extent1].[OtherProperty1] = @p__linq__0) AND ([Extent2].
[FirstDateTime] IS NOT NULL) AND ([Extent2].[FirstDateTime] = @p__linq__1)
) AS [Project1]
ORDER BY [Project1].[OtherProperty2] DESC, [Project1].[FirstDateTime] ASC,
[Project1].[Value] ASC',N'@p__linq__0 int,@p__linq__1
datetime2(7)',@p__linq__0=7,@p__linq__1='2016-10-11 11:45:53.6230000'
IQueryable
不保证顺序。 List<T>
确实如此。要保持 IQueryable
并保持顺序,您应该尝试对 IQueryable
进行排序,而不是尝试获得第一个结果:
result.OrderBy(y => y.FirstDateTime).FirstOrDefault(x => x.FirstDateTime == secondDateTime)
问题类似于this - looks like the LINQ provider generated a datetime value with a fractional second precision of 7 (e.g. 2016-10-11 11:45:53.6230000) while the column in SQL is defined as datetime, which has a precision lower than 7, as opposed to datetime2,可以达到7的精度
要解决此问题,您可以将 SQL 中的列类型从 datetime 更改为 datetime(7)