Reputation: 55
I have a list with DateTimes how can I count all the datetimes with the same day using linq?
I need to use the result to find out how many of one day there is compared to how many there should be.
Upvotes: 0
Views: 412
Reputation: 740
This will group on the day (by day I mean 1st, 2nd etc.) and ignore all other date parts and then return a list of each day and the number of times that day was found in the list
var dateInstances = listOfDates.GroupBy(x => x.Date.Day).Select(x => new {Day = x.Key, Instances = x.Count()}).ToList();
Upvotes: 0
Reputation: 460138
You could use GroupBy
on DateTime.Date
:
int count = dates.GroupBy(d => d.Date)
.Where(g => g.Count() > 1)
.Count();
This will count all DateTimes
in the list which date is not unique.
Upvotes: 4