这是我的尝试,看起来效率极低,因为迭代次数很多,而IsWorkingDay方法会查询数据库以查看该日期时间是否是公共假日。
有人能提出任何优化或替代方案吗?
public decimal ElapsedWorkingHours(DateTime start, DateTime finish)
{
decimal counter = 0;
while (start.CompareTo(finish) <= 0)
{
if (IsWorkingDay(start) && IsOfficeHours(start))
{
start = start.AddMinutes(1);
counter++;
}
else
{
start = start.AddMinutes(1);
}
}
decimal hours;
if (counter != 0)
{
hours = counter/60;
}
return hours;
}