Get the last 100 inserted records in sql server

c#, linq, sql-server, t-sql

Solution

How about

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

This should roughly translate to SQL

SELECT TOP 100 PKey
FROM testDetailRecords
ORDER BY PKey DESC

Problem

I want to retrieve the last 100 inserted records in sql server 2008. Please correct my code. Pkey in the table testContext.testDetailRecords is an identity column. ``` var pkeys = (from tests in testContext.testDetailRecords where tests.Pkey > (select max(tests.Pkey)-100 from testContext.testDetailRecords)) select tests.Pkey).ToList(); ```

Original source