Reputation: 37
My collection
"10/01/2016 4:00 PM"
"10/01/2016 11:00 AM"
"10/01/2016 12:00 PM"
I want to get : "10/01/2016 4:00 PM" since it is the current time and the latest at all. However when i use .Max() Function it returns the value of "10/01/2016 12:00 PM" which means it is just looking for the Highest Value of HOURS "12".
Upvotes: 1
Views: 1256
Reputation: 32445
You comparing string
values.
You need to convert your collection of strings to the collection of DateTime
var dateTimeCollection = stringCollection.Select(value => DateTime.Parse(value));
Then use Max
method
var maxDate = dateTimeCollection.Max();
Upvotes: 2
Reputation: 357
Which kind of Collection do you use? I had testet it with following Code
static void Main(string[] args)
{
List<DateTime> list = new List<DateTime>()
{
DateTime.Parse("10/01/2016 4:00 PM") ,
DateTime.Parse("10/01/2016 11:00 AM"),
DateTime.Parse("10/01/2016 12:00 PM")
};
Console.WriteLine(list.Max());
Console.ReadKey();
}
And it works
Upvotes: 0