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?


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.






© 2024 - ErnesTech - Privacy
E-Commerce Return Policy