Reputation: 1
Hey I want to find the lowest date after DateTime.Now from my saved times. My Idea was to add them all to a list then delete all 'DateTimes' from the list which are lower than the actual time to then find the lowest date in the list. The way I approached it doesnt work and I dont really understand why.. Here's my code:
List<DateTime> list = new List<DateTime>() {
Convert.ToDateTime(search.ReadConfig("somedate1")),
Convert.ToDateTime(search.ReadConfig("somedate2")),
Convert.ToDateTime(search.ReadConfig("somedate3")),
Convert.ToDateTime(search.ReadConfig("somedate5"))};
DateTime dtnow = DateTime.Now;
for(int kk = 0; kk < list.Count; kk++){
if(list[kk] < dtnow){ list.RemoveAt(kk); }
}
DateTime smallestDate = list.Min();
label2.Text = smallestDate.ToString();
It doesnt remove the times older than now and I wasnt able to find something which fits my problem over google. I would appreciate any help!
Upvotes: 0
Views: 412
Reputation: 16049
Try Where()
clause,
Filters a sequence of values based on a predicate.
//Here you have to use Greater than equal operator.
var minDate = list.Where(x => x >= DateTime.Now).Min().ToString();
Console.WriteLine(minDate);
Upvotes: 1