我有一個使用 linq 的 C# 查詢,我試圖比較兩個日期時間欄位。Postgres DB 中的 datetime 欄位存盤為帶有時區的時間戳。但是由于某種原因,我收到“函式 date_trunc(未知,時區時間戳,未知)不存在”錯誤,盡管在除錯視圖中它顯示我正在傳遞正確的引數。請注意:MyDate 欄位為 Nullable
C#
_context.MyDbObject.Where(a => DateTime.UtcNow.Date >= a.MyDate.Value.Date).AsQueryable();
除錯視圖中顯示的查詢
(date_trunc('day', now(), 'UTC') >= date_trunc('day', a."MyDate", 'UTC')))
錯誤
MessageText: function date_trunc(unknown, timestamp with time zone, unknown) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
uj5u.com熱心網友回復:
這不是您問題的直接答案,但如果您關心資料庫索引,請不要在查詢中使用日期截斷。您的查詢可以重寫。
var currentDate = DateTime.UtcNow.Date;
var endDate = currentDate.AddDays(1);
var query = _context.MyDbObject
.Where(a => a.MyDate.Value < endDate)
.AsQueryable();
// or records for current day
var query = _context.MyDbObject
.Where(a => a.MyDate.Value >= currentDate && a.MyDate.Value < endDate)
.AsQueryable();
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/514702.html
下一篇:SQL陳述句中的條件
