c# - 在sql server中获取最后100条插入的记录

标签 c# sql-server linq tsql

我想在 sql server 2008 中检索最后 100 条插入的记录。

请更正我的代码。 表 testContext.testDetailRecords 中的 Pkey 是标识列。

var pkeys = (from tests in testContext.testDetailRecords
                         where tests.Pkey > (select max(tests.Pkey)-100 from testContext.testDetailRecords))
                                select tests.Pkey).ToList();

最佳答案

怎么样

var pkeys = testContext.testDetailRecords
                 .OrderByDescending(x => x.PKey)
                 .Take(100)
                 .Select(x => x.PKey);

这应该大致翻译成 SQL

SELECT TOP 100 PKey
FROM testDetailRecords
ORDER BY PKey DESC

关于c# - 在sql server中获取最后100条插入的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13401769/

相关文章:

sql - 基于 SQL Server 中另一个查询的 Where 子句

sql-server - hive 中的 rank()

sql-server - Sql 调试查询与 Management Studio 中的调试同时运行 - 锁定数据会挂起查询

c# - 在 C# 中,从列表中的列表中删除一个元素。

c# - 如何在 NotifyIcon 中使用 MouseWheel

C# UWP Visual Studio 2017 单元测试

c# - C#Word自动化使Windows资源管理器崩溃

c# - System.Text.RegularExpressions.Regex.Matches 的性能特征 - 一个错误?

Linq PredicateBuilder 多条件

.net - 将表达式<Func<FromType>> 转换为表达式<Func<ToType>>