3

我想检索 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();
4

3 回答 3

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
于 2012-11-15T16:24:34.283 回答
2
var pkeys = (from tests in testContext.testDetailRecords
             orderby tests.Pkey descending
             select tests.Pkey).Take(100).ToList();
于 2012-11-15T16:24:34.103 回答
0
var result = testContext.testDetailRecords.OrderByDescending(tests.Pkey).Take(100).Select(...);
于 2012-11-15T16:24:17.413 回答