我在LINQ一個簡單的查詢SQL:如何強制LINQ to SQL將.NET DateTime作爲datetime2傳遞?
var id = 23479824;
var date = Changes.Where(ch => ch.Id == id).First().Date;
var diff = Changes.Where(ch => ch.Id == id).Select(ch => SqlMethods.DateDiffNanosecond(date, ch.Date)).First();
的diff
變量應該是零,但並非如此。生成的SQL是以下:
DECLARE @p0 DateTime = '2010-07-14 15:05:49.207'
DECLARE @p1 Int = 44633
SELECT TOP (1) [t1].[value]
FROM (
SELECT DATEDIFF(Nanosecond, @p0, [t0].[Date]) AS [value], [t0].[Id]
FROM [dbo].[Changes] AS [t0]
) AS [t1]
WHERE [t1].[Id] = @p1
GO
我發現,它是由LINQ引起SQL被傳遞.NET DateTime
類型作爲SQL datetime
。但是Date
列的SQL類型爲datetime2
。
這是LINQ到SQL或功能的錯誤嗎?如何強制LINQ to SQL將輸入日期作爲datetime2
?
有什麼聰明的方法來查詢動態查詢中的特定參數(事先不知道)?或者,我必須用'datetime2'替換所有'datetime'? – 2010-07-14 20:47:12