c# - FindAsync 返回最近的 1000 个结果

标签 c# asp.net-mvc entity-framework asynchronous

我正在编写一个测试来检查应用程序日志,每分钟可能有数千条日志。我想使用 FindAsync 获取最新的日志。但有时服务会中断,因此使用时间值并不准确:

// may return many or none` 
context.FindAsync(x => x.Timestamp >= DateTime.Now.AddMinutes(-10))

我想做的是:

context.FindAsync(x => OrderByDescending(x.Timestamp).Take(1000))

问题是,如果 FindAsync 返回太多结果,它可能会减慢其他服务和线程的速度。有没有办法使用 FindAsync 只返回最近的 1000 行?

最佳答案

我觉得你想多了。在 SQL 中,您将SELECT TOP 1000ORDER BY Timestamp DESC。在这里做同样的事情:

await context.Logs.OrderByDescending(x => x.Timestamp).Take(1000).ToListAsync();

关于c# - FindAsync 返回最近的 1000 个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41489204/

相关文章:

c# - MongoDB C# 驱动程序 - 更新嵌入式文档数组中的所有字段

c# - 可以通过浏览器访问 WCF 服务,但不能通过 PowerShell 中的 GET

c# - 重定向到 URL 而不是未经身份验证的 401

c# - Razor - 使用 foreach,每第 n 个项目插入 html

entity-framework - 指定外键属性的 EF Code First Fluent API

c# - 如何解决 Azure "Windows logins are not supported in this version of SQL Server"?

c# - 一起添加列表

c# - 如何防止托管 dll 对 C# 进行逆向工程

javascript - 数据表 YADCF 重置所有列时出错 (exResetAllFilters)

entity-framework - Entity Framework 生成低效的 SQL