max date record in LINQ

Starting from .NET 6 MaxBy LINQ method is available.

var result = items.MaxBy(i => i.Date);

Prior to .NET 6:

O(n log n):

var result = items.OrderByDescending(i => i.Date).First();

O(n) – but iterates over the sequence twice:

var max = items.Max(i => i.Date);
var result = items.First(i => i.Date == max);

Or you can use MoreLINQ which has MaxBy method which is O(n)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)