【发布时间】:2019-10-03 14:16:31
【问题描述】:
我正在尝试从表中返回前 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 行。对我犯了什么错误有什么建议吗?
【问题讨论】:
-
尚不清楚您的代码已经从表中仅返回 1000 行的问题是什么
-
试过
var entries = _DbContext.ReportNewEntries.Take(1000).OrderBy(a => long.Parse(a.Id)).Where(a => long.Parse(a.Id) > id);? -
@AhmedYousif 问题清楚地表明问题不是代码返回的内容,而是它在下面运行的查询。
-
它不应该,因为在尝试解决它或调用 .ToList() stackoverflow.com/questions/2656576/… 之前该语句不会执行
-
@AhmedYousif - 但确实如此,我正在运行 sql profiler,而且确实如此。
标签: c# entity-framework .net-core entity-framework-core