How to get the latest date from a table

I have code where I am querying something from my table that has the CreateDate property. I just want to get the object with the most recent date. This is what I have:

 var post = UnitOfWork.TableName.Query(postFilter); 

I tried using the Max function as follows:

 var post = UnitOfWork.TableName.Query(postFilter).Max(x => x.CreatedDate); 

but returns only the date.

How do I return the entire object that is the most recent?

+4
source share
1 answer

You need to place an order by date and do one thing:

 var post = UnitOfWork.TableName.Query(postFilter) .OrderByDescending(x => x.CreatedDate) .FirstOrDefault(); 
+7
source

Source: https://habr.com/ru/post/1468832/


All Articles