I have a generic list of items. Each item contains a DateTime field. I would like to find the newest item in the list using Linq in the most elegant and efficient way.
Elegance is more important than efficiency in my case, but doing this also in an efficient way would be nice.
Thank you.
After reading the answers: Here is the code (and the answer I liked):
using System.Collections.Generic;
using System.Linq;
class Item
{
public Item Date { get; set; }
public string Name { get; set; }
}
static void Main(string[] args)
{
List<Item> items = CreateItems();
Item newest;
if (items.Count == 0)
newest = null;
else
newest = items.OrderByDescending(item => item.Date).First();
}