How to get the last record of Entity Framework model data

Question: How do you get a recent record using EF Core C# Query? I would like to retrieve a most recent data record from the database but the query returns the last record. How does this work?

Please Login to see the rest of the answer

Answer: If you want to get the most recent record from the database using EF Core or Entity Framework then you must order the result then get FirstOrDefault() record from the dataset returned. See the code below:

var MyRecentRecord = _context.MyDataModalOrPOCO.Where(a => a.MyLovelyPredicateHere).OrderByDescending(b=>b.MyLovelyPropertyNameHere).FirstOrDefault(); //This will retrieve the most recent data node from the database.

:) Good Luck!!

If this does not solve your problem check out this article here.

Write an Article
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.

© 2022 - ErnesTech - Privacy