[英]EF Core equivalent of the LINQ query
我在 c# 中有一個邏輯,我應該根據兩個日期時間列之間的差異過濾記錄
var users = repository.GetAll().Where
(
userType => userType.CreatedTimeStamp.HasValue &&
userType.UpdatedTimeStamp.HasValue &&
userType.UpdatedTimeStamp.Value.Subtract(userType.CreatedTimeStamp.Value).TotalHours > 6 &&
userType.UpdatedTimeStamp.Value.Subtract(userType.CreatedTimeStamp.Value).TotalHours < 150
);
從存儲庫中獲取所有方法
public virtual IQueryable<TEntity> GetAll()
{
var obj = dataContext.Model.GetEntityTypes();
return dataContext.Set<TEntity>();
}
但是,根據我在 mini-profiler 上看到的內容,查詢實際上並沒有在服務器上執行
SELECT "userType".usertypeid, "userType".created_by, "userType".created_ts, "userType".isdelete, "userType".updated_by, "userType".updated_ts, "userType".usertypecode, "userType".usertypename
FROM usr.usertype AS "userType"
我試過這個,它也不起作用
var users = repository.GetAll().Where
(
userType => userType.CreatedTimeStamp.HasValue &&
userType.UpdatedTimeStamp.HasValue &&
EF.Functions.DateDiffHour(userType.CreatedTimeStamp.Value, userType.UpdatedTimeStamp.Value) > 6
);
EF 核心版本 2.2.4
ASP.NET 內核2.2
只是想知道如何在 EF-Core 中實現這一點
更新 1:
如果我使用普通過濾器,它會按預期執行,只是把它放在一邊
SELECT "userType".usertypeid, "userType".created_by, "userType".created_ts, "userType".isdelete, "userType".updated_by, "userType".updated_ts, "userType".usertypecode, "userType".usertypename
FROM usr.usertype AS "userType"
WHERE ("userType".created_ts IS NOT NULL AND "userType".updated_ts IS NOT NULL) AND ("userType".usertypecode = 'INT')
更新 2:
為存儲庫添加代碼獲取所有方法
更新 3:我的后端是 Npgsql.EntityFrameworkCore.PostgreSQL
更新 4:喬希的回答也不起作用
var users = dataContext.UserTypes.Where
(
userType => userType.CreatedTimeStamp.HasValue &&
userType.UpdatedTimeStamp.HasValue &&
EF.Functions.DateDiffHour(userType.CreatedTimeStamp.Value, userType.UpdatedTimeStamp.Value) > 6 &&
EF.Functions.DateDiffHour(userType.CreatedTimeStamp.Value, userType.UpdatedTimeStamp.Value) < 150
);
return Ok(users.ToList());
你需要扭轉你做這件事的方式。
var users = repository.Where
(
userType => userType.CreatedTimeStamp.HasValue &&
userType.UpdatedTimeStamp.HasValue &&
EF.Functions.DateDiffHour(userType.CreatedTimeStamp.Value,
userType.UpdatedTimeStamp.Value) > 6
).GetAll();
偽代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.