I’ve found some weird, wonderful and ridiculously complicated LINQ queries for getting the row with MAX(DATE) based on a key. Most unnecessarily use lambda expressions, and some just had several interim steps. I knew there had to be a better way, and found an unassuming post at the bottom of a StackOverflow page.
Here’s my non-lambda’d, contrived example… assuming you’ve got an EntityFramework model (i.e. context)
//Get Client Order with (max) order date
var maxclientOrder = (from clientOrder in context.ClientOrders
where clientOrder.OrderDate ==
(from clientOrder2 in context.ClientOrders
where clientOrder2.ClientID == clientOrder.ClientID
select clientOrder2.OrderDate).Max()
select clientOrder).ToList();