简体   繁体   中英

How to get the most recent date from a table

I have code where I'm querying something from my table which has a property called CreateDate . 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 a Max function like this:

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

but that returns just the date.

How do I return a whole object that is the most recent?

You have to order by the date and take one:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM