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?

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.

Edit this Article
database Entity Framework EF Core AspNet 6 SQL .NET 6 published
Was this page helpful?
Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful.. Leave ErnesTech Feedback.Feedback
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.
Your Session is Ending
Login to Continue

© 2021 - ErnesTech - Privacy