EDIT: Okay, now I see more about what you are trying to achieve.
So you have an input:
- Start Date and End Date
- List of pairs month / day. Currently, you actually do not have this, but that is what you logically received.
Your current code does not take into account the fact that it can be several years.
First modify your data table accordingly:
var monthDays = dateTypeList.Rows.AsEnumerable() .Select(row => new { Month = row.Field<int>("Month") Day = row.Field<int>("Day") }) .ToList();
Now you can use:
for (int year = startDate.Year; year <= endDate.Year; year++) { foreach (var pair in monthDays) {
This is an immediate problem:
for (DateTime a = dateStart; a <= dateEnd; a.AddDays(1))
DateTime is unchanged, so AddDays does not work unless you use the return value. Do you want to:
for (DateTime a = dateStart; a <= dateEnd; a = a.AddDays(1))
I would also modify this code:
dateDates[i] = Convert.ToDateTime( Convert.ToDateTime(string.Format("{0}/{1}", Convert.ToInt32(dateTypeList.Rows[i]["Month"]), Convert.ToInt32(dateTypeList.Rows[i]["Day"]))));
- This happens via string representation for no reason.
- It calls
Convert.ToDateTime twice for no reason - The system date format is assumed to be month / day, which is culture specific.
source share