Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Earliest date from from IEnumerable<DateTime>

I have an IEnumerable<DateTime> with a number of dates in it. How would I get the earliest date from that collection?

Thanks!

Dave

like image 690
Dave Avatar asked Mar 10 '11 16:03

Dave


2 Answers

var earliest = collection.Min();
like image 194
Oleks Avatar answered Oct 25 '22 02:10

Oleks


You can do this immediately with LINQ using Enumerable.Min.

DateTime minDate = dateCollection.Min();

Since DateTime implements IComparable<DateTime>, Enumerable.Min will use DateTime.CompareTo to find the minimum DateTime in the collection.

like image 43
jason Avatar answered Oct 25 '22 01:10

jason