Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select last inserted ten rows

I am using Entity Framework. I want to get recently inserted (means last ten) ten rows. The table User has two columns:

userID
password
DateTime

How can I get most recent ten rows?

like image 387
Fakhar uz Zaman Avatar asked Feb 24 '26 23:02

Fakhar uz Zaman


1 Answers

If you have a DateTime (or DATE) column then you can use something like this:

using(YourDbContext ctx = new YourDbContext())
{
   var lastTenRows = ctx.Users.OrderByDescending(u => u.DateTimeColumn).Take(10).ToList();
} 
like image 78
marc_s Avatar answered Feb 27 '26 03:02

marc_s