向不在 While 循环中执行的 C# 可空 DateTime 对象添加天数
Adding Days To C# Nullable DateTime Object Not Performing In While Loop
我正在这里编写一些生产代码,并且不得不在已经有点问题的代码之上添加一些我自己设计的代码。我有一个循环可以帮助根据特定的支付期计算日期。
我遇到的问题是,当使用 DateTime?.Value.AddDays()
函数调用向它们添加值时,我的 Nullable DateTime
对象根本没有改变。当我 运行 通过此代码时, nextPayDate
对象及其值保持不变,即使我在循环期间添加到它也是如此。上述行为没有用,而且会造成无限循环。想法?
public static List<DateTime?> GetLPSPaymentDates(Applicant applicant, int loanTermType, DateTime deliveryConfirmationDate)
{
if (applicant == null)
{
throw new Exception("The applicant you are wanting to set an LPS for is null");
}
DateTime? firstPaymentDate = DateTime.Today;
DateTime? secondPaymentDate = DateTime.Today;
DateTime? lastPayDate = applicant.MainEmployer.LastPayDate;
DateTime? nextPayDate = applicant.MainEmployer.NextPayDate;
DateTime? earliestPossiblePaymentDate = deliveryConfirmationDate.AddDays(10);
int daysToAdd = 0;
if (lastPayDate != null && nextPayDate != null)
{
var tempDate = nextPayDate;
while (tempDate.Value <= earliestPossiblePaymentDate.Value)
{
daysToAdd = IncrementPaymentDateByTermType((TermTypes)loanTermType);
nextPayDate.Value.AddDays(daysToAdd);
firstPaymentDate.Value.AddDays(daysToAdd);
}
if (firstPaymentDate == DateTime.Today)
{
firstPaymentDate = nextPayDate;
}
secondPaymentDate = firstPaymentDate.Value.AddDays(daysToAdd);
}
else
{
throw new Exception("Could not find applicant's last or next pay date.");
}
List<DateTime?> firstAndSecondDates = new List<DateTime?>()
{
firstPaymentDate,
secondPaymentDate
};
return firstAndSecondDates;
}
public static int IncrementPaymentDateByTermType(TermTypes termType)
{
int numberOfDaysToAdd;
switch (termType)
{
case TermTypes.BiWeekly:
numberOfDaysToAdd = 14;
break;
case TermTypes.Weekly:
numberOfDaysToAdd = 7;
break;
default:
numberOfDaysToAdd = 1;
break;
}
return numberOfDaysToAdd;
}
没有测试,但这应该会有所帮助(假设你的 while 循环逻辑是正确的)
while (tempDate.Value <= earliestPossiblePaymentDate.Value)
{
daysToAdd = IncrementPaymentDateByTermType((TermTypes)loanTermType);
tempDate = nextPayDate.Value.AddDays(daysToAdd); // <<= updating tempDate
firstPaymentDate.Value.AddDays(daysToAdd);
}
AddDays returns 一个新对象作为结果,而不是更新现有对象。尝试:
daysToAdd = IncrementPaymentDateByTermType((TermTypes)loanTermType);
nextPayDate = nextPayDate.Value.AddDays(daysToAdd);
firstPaymentDate = firstPaymentDate.Value.AddDays(daysToAdd);
我正在这里编写一些生产代码,并且不得不在已经有点问题的代码之上添加一些我自己设计的代码。我有一个循环可以帮助根据特定的支付期计算日期。
我遇到的问题是,当使用 DateTime?.Value.AddDays()
函数调用向它们添加值时,我的 Nullable DateTime
对象根本没有改变。当我 运行 通过此代码时, nextPayDate
对象及其值保持不变,即使我在循环期间添加到它也是如此。上述行为没有用,而且会造成无限循环。想法?
public static List<DateTime?> GetLPSPaymentDates(Applicant applicant, int loanTermType, DateTime deliveryConfirmationDate)
{
if (applicant == null)
{
throw new Exception("The applicant you are wanting to set an LPS for is null");
}
DateTime? firstPaymentDate = DateTime.Today;
DateTime? secondPaymentDate = DateTime.Today;
DateTime? lastPayDate = applicant.MainEmployer.LastPayDate;
DateTime? nextPayDate = applicant.MainEmployer.NextPayDate;
DateTime? earliestPossiblePaymentDate = deliveryConfirmationDate.AddDays(10);
int daysToAdd = 0;
if (lastPayDate != null && nextPayDate != null)
{
var tempDate = nextPayDate;
while (tempDate.Value <= earliestPossiblePaymentDate.Value)
{
daysToAdd = IncrementPaymentDateByTermType((TermTypes)loanTermType);
nextPayDate.Value.AddDays(daysToAdd);
firstPaymentDate.Value.AddDays(daysToAdd);
}
if (firstPaymentDate == DateTime.Today)
{
firstPaymentDate = nextPayDate;
}
secondPaymentDate = firstPaymentDate.Value.AddDays(daysToAdd);
}
else
{
throw new Exception("Could not find applicant's last or next pay date.");
}
List<DateTime?> firstAndSecondDates = new List<DateTime?>()
{
firstPaymentDate,
secondPaymentDate
};
return firstAndSecondDates;
}
public static int IncrementPaymentDateByTermType(TermTypes termType)
{
int numberOfDaysToAdd;
switch (termType)
{
case TermTypes.BiWeekly:
numberOfDaysToAdd = 14;
break;
case TermTypes.Weekly:
numberOfDaysToAdd = 7;
break;
default:
numberOfDaysToAdd = 1;
break;
}
return numberOfDaysToAdd;
}
没有测试,但这应该会有所帮助(假设你的 while 循环逻辑是正确的)
while (tempDate.Value <= earliestPossiblePaymentDate.Value)
{
daysToAdd = IncrementPaymentDateByTermType((TermTypes)loanTermType);
tempDate = nextPayDate.Value.AddDays(daysToAdd); // <<= updating tempDate
firstPaymentDate.Value.AddDays(daysToAdd);
}
AddDays returns 一个新对象作为结果,而不是更新现有对象。尝试:
daysToAdd = IncrementPaymentDateByTermType((TermTypes)loanTermType);
nextPayDate = nextPayDate.Value.AddDays(daysToAdd);
firstPaymentDate = firstPaymentDate.Value.AddDays(daysToAdd);