我想计算两个给定日期之间一周中每一天的出现次数。
例如:
从 20/07/2014 到 27/7/2014,为期 8 天,有:
周日=2,周一=1,周二=1,...
请您参考如下方法:
试试这个:
DateTime start = new DateTime(2014,07,20);
DateTime end = new DateTime(2014,07,27);
TimeSpan ts = end - start;
int limit = ts.Days;
var result = Enumerable.Range(0,limit+1)
.Select(x => start.AddDays(x))
.GroupBy(x => x.DayOfWeek)
.Select(x => new {day = x.Key, count = x.Count()});
我们创建一个从开始到结束的日期范围,包括这两个日期,然后按星期几分组以获得天数和相应的计数。
