I'm comparing records based on their dates, if the date is equal to a particular month its data goes to a particular object. Example: If this the record is in the current month then it will be saved in the current month field. If the record is in the previous month then it will also be saved in the previous month field. Now, I have two sets of code snippets. Which is heavy when being executed:
First Snippet: (declaring variables)
DateTime prevDate = currentDate.AddMonths(-1),
nextDate = currentDate.AddMonths(1),
thirdDate = currentDate.AddMonths(2),
fourthDate = currentDate.AddMonths(3);
if (prevDate .Month == invoiceDetail.InvoiceDate.Month
&& prevDate.Year == invoiceDetail.InvoiceDate.Year)
{
//code here
}
if (nextDate.Month == invoiceDetail.InvoiceDate.Month
&& nextDate.Year == invoiceDetail.InvoiceDate.Year)
{
//code here
}
if (thirdDate.Month == invoiceDetail.InvoiceDate.Month
&& thirdDate.Year == invoiceDetail.InvoiceDate.Year)
{
//code here
}
if (prevDate .Month == invoiceDetail.InvoiceDate.Month
&& prevDate.Year == invoiceDetail.InvoiceDate.Year)
{
//code here
}
Second Snippet: (directly insert them in the if statement)
if (currentDate.AddMonths(-1).Month == invoiceDetail.InvoiceDate.Month
&& currentDate.AddMonths(-1).Year == invoiceDetail.InvoiceDate.Year)
{
//code here
}
if (currentDate.AddMonths(1).Month == invoiceDetail.InvoiceDate.Month
&& currentDate.AddMonths(1).Year == invoiceDetail.InvoiceDate.Year)
{
//code here
}
if (currentDate.AddMonths(2).Month == invoiceDetail.InvoiceDate.Month
&& currentDate.AddMonths(2).Year == invoiceDetail.InvoiceDate.Year)
{
//code here
}
if (currentDate.AddMonths(3).Month == invoiceDetail.InvoiceDate.Month
&& currentDate.AddMonths(3).Year == invoiceDetail.InvoiceDate.Year)
{
//code here
}