我正在嘗試從 Azure 中的資料庫檢索記錄并進行檢查,我直接運行 T-SQL 查詢,然后針對 EF Core 背景關系將它們傳遞給 LINQ,但我遇到了這個問題。

select FechaOrientativa,id, Archivo, Estado, Estudiar, Descripcion
from Concursos
where FechaOrientativa>=CAST( GETDATE() AS Date ) and Estudiar='pt'
order by FechaOrientativa, Archivo, Estado
當我過濾FechaOrientativa大于或等于今天的記錄時,該Estudiar欄位等于'pt',我得到 2,296 條記錄。
現在在 Angular 中,我執行 http.Get 到我的 Web API 并執行以下操作:
[HttpGet("sintratar")]
public async Task<ActionResult<IEnumerable<Concurso>>> GetConcursosSinTratar()
{
return await _context.Concursos.Where(c => c.Estudiar == "pt" && c.FechaOrientativa >= DateTime.Now).OrderBy(c => c.FechaOrientativa).ToListAsync();
}
令我驚訝的是,我只收到了 2,151 條記錄,卻找不到任何解釋。
請問有什么想法嗎?
謝謝。
uj5u.com熱心網友回復:
比較 SQL 查詢和 EF LINQ 查詢,區別在于:
SQL -CAST( GETDATE() AS Date )回傳沒有時間的今天日期。
EF LINQ -DateTime.Now回傳當前日期時間。
因此查詢的結果會有所不同
(例如:僅查詢日期時間欄位等于/晚于查詢日期時間的記錄)。
從日期和時間函式中,您正在尋找DateTime.Today
| 日期時間.今天 | 轉換(日期,GETDATE()) |
return await _context.Concursos
.Where(c => c.Estudiar == "pt" && c.FechaOrientativa >= DateTime.Today)
.OrderBy(c => c.FechaOrientativa)
.ToListAsync();
或者,您可以將 EF LINQ 中的 SQL 查詢與.FromSqlRaw().
return await _context.Concursos
.FromSqlRaw(@"select FechaOrientativa,id, Archivo, Estado, Estudiar, Descripcion
from Concursos
where FechaOrientativa>=CAST( GETDATE() AS Date ) and Estudiar='pt'
order by FechaOrientativa, Archivo, Estado")
.ToList();
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/491963.html
標籤:sql 林克 tsql 实体框架核心 asp.net-core-webapi
上一篇:DB2:如何忽略例外?
