我正在研究这个员工绩效考核应用程序。当用户在15个工作日结束前离开3个工作日和1个工作日时,我需要发送提醒电子邮件。
以下是我计划的方式: 我有每个评论的上次修改日期,我应该可以使用它来查找未来15个工作日(我们的例子称之为deadLineToSubmit)。一旦我死了LineToSubmit我应该检查当前日期+ 3天(日期)== deadLineToSubmit然后将该评论添加到我将用于发送电子邮件的列表中。
我有一个名为GetByDayPrior的函数,我发送值3和1来检查用户是否已离开3个工作日或1.下面是代码:
public List<int> GetByDaysPrior(int daysPrior)
{
List<int> response = new List<int>();
using (DAL.HumanResourcesEntities context = new DAL.HumanResourcesEntities())
{
DateTime currentDate = DateTime.Now.Date;
var lastModifiedDate = context.Reviews
.Where(s => s.IsActive == false)
.Where(s => s.ReviewStatusId == (int)ReviewStatuses.EmployeeSignature)
.Select(v => v.ModifiedDate)
.ToList();
AddBusinessDays(lastModifiedDate, daysPrior);
var lastRuntime = context.ApplicationRuntimeVariables.Where(y => y.ParameterName == "RemindersSentDT").Select(x => x.ParameterValue).FirstOrDefault().Date;
DateTime deadLineToSubmitReview = currentDate.AddDays(daysPrior).Date;
if (lastRuntime.AddHours(24).Date <= currentDate) //should it be == current date ?
{
var reviewIDs = context.Reviews
.Where(s => s.ModifiedDate < deadLineToSubmitReview)
.Where(s => s.ReviewStatusId == (int)ReviewStatuses.EmployeeSignature) //Check to make sure reminders only go when Review Status is Employee Signature
.Where(s => s.IsActive == false)
.Select(v => v.ReviewId)
.ToList();
response = reviewIDs.ToList();
}
return response;
}
}
我有另一个名为AddBusinessDays的函数,当我传入LastModifiedDate和15天作为参数时,应该给我deadLineToSubmit。但目前它无法正常工作,因为首先我无法找到如何在此函数中将List of DataTime作为参数传递。此外,当我在AddBusinessDays中将日期参数定义为List时,所有date和DayOfWeek的实例都是平面的
“错误32无法隐式转换类型 'System.Collections.Generic.List'到 '的System.DateTime'
以下是我的AddBusinessDays函数。
public static DateTime AddBusinessDays(List<DateTime> date, int days)
{
if (days < 0)
{
throw new ArgumentException("days cannot be negative", "days");
}
if (days == 0) return date;
if (date.DayOfWeek == DayOfWeek.Saturday)
{
date = date.AddDays(2);
days -= 1;
}
else if (date.DayOfWeek == DayOfWeek.Sunday)
{
date = date.AddDays(1);
days -= 1;
}
date = date.AddDays(days / 5 * 7);
int extraDays = days % 5;
if ((int)date.DayOfWeek + extraDays > 5)
{
extraDays += 2;
}
return date.AddDays(extraDays);
}
以下是我需要了解的内容:如何在AddBusinessDays函数中将DateTime List作为参数发送。如何使AddBusinessDays函数工作,因为现在我们传递的是list而不是单个dateTime。我的思维逻辑是否正确解决了这个问题?
非常感谢! :)
答案 0 :(得分:1)
日期是一个列表,而不是一个日期
您可以使用Select
对列表中的所有项目进行操作
例如:
lastModifiedDate = AddBusinessDays(lastModifiedDate, daysPrior);
第1步:将您的功能转换为接收单个日期
private static DateTime AddBusinessDays(DateTime date, int days)
{
if (days < 0)
{
throw new ArgumentException("days cannot be negative", "days");
}
if (days == 0) return date;
if (date.DayOfWeek == DayOfWeek.Saturday)
{
date = date.AddDays(2);
days -= 1;
}
else if (date.DayOfWeek == DayOfWeek.Sunday)
{
date = date.AddDays(1);
days -= 1;
}
date = date.AddDays(days / 5 * 7);
int extraDays = days % 5;
if ((int)date.DayOfWeek + extraDays > 5)
{
extraDays += 2;
}
return date.AddDays(extraDays);
}
步骤2:使用单日期功能作为日期列表
public static List<DateTime> AddBusinessDays(List<DateTime> date, int days)
{
return date.Select(d => AddBusinessDays(d, days)).ToList();
}