Dave
Dave

Reputation: 2562

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

Upvotes: 11

Views: 7930

Answers (2)

Oleks
Oleks

Reputation: 32323

var earliest = collection.Min();

Upvotes: 14

jason
jason

Reputation: 241621

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.

Upvotes: 20

Related Questions