![](/img/trans.png)
[英]Transition EF Core 3.1 to storing string enum values, rather than ints
[英]Full Dataset rather than Top 1000 EF Core
我試圖從表中返回前1000行,但是使用EF Core,我在.Take(1000)運行之前獲取完整的數據集。
API
[HttpGet("MyThing/{id}")]
public List<MyObj> ItemsReport(long id)
{
response = new List<MyObj>();
response = _reporting.GetNewEntries(id);
return response;
}
報告
public virtual List<MyObj> GetNewEntries(long id)
{
var newEntries = new List<MyObj>();
var entries = _DbContext.ReportNewEntries
.OrderBy(a => long.Parse(a.Id))
.Where(a => long.Parse(a.Id) > id)
.Take(1000);
newEntries.AddRange(entries);
return newEntries;
}
的DbContext
DbSet<MyObj> ReportNewEntries{ get; set; }
我想要的查詢是這樣的:
SELECT TOP (1000) *
FROM dbo.ReportNewEntries
WHERE CONVERT(bigint, Id) > 0
ORDER BY CONVERT(bigint, Id)
根據SQL分析器,我當前的查詢是:
SELECT * FROM dbo.ReportNewEntries
該表是單片的,所以我想一次訪問行1000。 對我犯的錯誤有什么建議嗎?
當您使用List
,您正在使用Linq-To-Object ,它可以在IEnumerable
。
事實上, IEnumerable
將在應用Take之前首先加載內存中的所有數據。
你想要使用的是IQueryable
,它是Linq-to-Sql 。
因此,要么修改方法以返回IQueryable<MyObj>
,要么使用AsQueryable()
調用Linq鏈,如下所示:
var entries = _DbContext.ReportNewEntries.AsQueryable()
.OrderBy(a => long.Parse(a.Id)).Where(a => long.Parse(a.Id) > id).Take(1000);
你用的是什么數據庫? 什么版本的.NetCore? 什么db提供商? 某些db提供程序可能在將一些linq語句轉換為sql時遇到問題。 在這種情況下,將在數據庫中運行不同的查詢(通常獲取所有數據)並在內存中執行linq。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.