我想在sql server 2008中检索最后100个插入的记录。
请更正我的代码。 表中的Pkey testContext.testDetailRecords是一个标识列。
var pkeys = (from tests in testContext.testDetailRecords
where tests.Pkey > (select max(tests.Pkey)-100 from testContext.testDetailRecords))
select tests.Pkey).ToList();
答案 0 :(得分:5)
怎么样
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
答案 1 :(得分:2)
var pkeys = (from tests in testContext.testDetailRecords
orderby tests.Pkey descending
select tests.Pkey).Take(100).ToList();
答案 2 :(得分:0)
var result = testContext.testDetailRecords.OrderByDescending(tests.Pkey).Take(100).Select(...);