/// <summary>
/// 根据时间段获取每星期X的总数
/// </summary>
/// <param name="beginDateTime">开始时间</param>
/// <param name="endDateTime">结束时间</param>
/// <param name="week">星期几 0:星期日</param>
/// <returns></returns>
public static int GetWeekCount(DateTime beginDateTime, DateTime endDateTime, List<DayOfWeek> weekList)
{
}
如何让这个单元测试相等呀?
List<DayOfWeek> list = new List<DayOfWeek>();
list.Add(DayOfWeek.Tuesday);
list.Add(DayOfWeek.Wednesday);
//从2012-5-1日到2012-5-31 日有10天符合星期二或星期三 TestHelper.Test(DateTimeHelper.GetWeekCount(DateTime.Parse("2012-5-1"), DateTime.Parse("2012-5-31"), list) == 10);
public static int GetWeekCount(DateTime begin, DateTime end, List<DayOfWeek> weekList) { return Enumerable.Range(0, (end - begin).Days) .Count(n => weekList.Contains(begin.AddDays(n).DayOfWeek)); }
改成这个了,谢谢
public static int GetWeekCount(DateTime begin, DateTime end, List<DayOfWeek> weekList){ return Enumerable.Range(0, (end.AddDays(1) - begin).Days) .Count(n => weekList.Contains(begin.AddDays(n).DayOfWeek));}
但是性能还是比较低
获取一段时间内有多少个周几,你这样写试试呢:
public static int GetWeekCount(DateTime beginDateTime, DateTime endDateTime, List<DayOfWeek> weekList)
{
int num = 0;
for (; beginDateTime < endDateTime; beginDateTime = beginDateTime.AddDays(1))
{
foreach ( DayOfWeek dow in weekList)
{
if (beginDateTime.DayOfWeek == dow)
num++;
}
}
return num;
}
这样的话性能不太好吧。