I am trying to compare a list AAA that contain Date with a range of dates. I want to see if any of the range of date is present in the list or not. If the date is present I copy the list items to another list BBB else I add empty values to the list BBB.
The problem I am having is that with my actual code, is I don`t know how not pass through the false statement of the while loop, till it reaches the end of the comparison.
With the code below, it is passing both the true and false in the while loop, which is falsifying the required result. The result I am obtaining is for every time that is present, I am having the same time as false. In short, lets say the list contains the date 6/5/2010, and the range of date is 4/5/2010 to 7/5/2010. so I will have an item created in the true part and AN ITEM CREATED INTHE FALSE PART, which is wrong. The date present can either be in true or false part. Not both, such that I have two items bing created!
How can I achieve the right result? Any other method or suggetsion please.
My code is as follows:
DateTime StartDate;
DateTime EndDate;
Datetime tempDate = StartDate;
List<DateTime> dateToEvaluate;
bool TimeIsPresent = false;
foreach (var tempItem in TaskList)
{
while (EndDate.AddDays(1) != tempDate)
{
if (tempItem.Date[0] == tempDate)
{
TimeIsPresent = True;
break;
}
else
{
if (TimeIsPresent == False)
{
if (!(tempDate.DayOfWeek == DayOfWeek.Sunday)
{
dateToEvaluate = new List<DateTime>();
dateToEvaluate.Add(tempDate);
tempTask.Add(new GroupedTask { ID = null,
TaskID = null,
Date = dateToEvaluate });
}
}
}
tempDate = tempDate.AddDays(1);
}
if (TimeIsPresent == True)
{
tempTask.Add(new GroupedTask { ID = tempItem.ID,
TaskID = tempItem.TaskID,
Date = tempItem.Date });
TimeIsPresent = false;
}
}
let me give you an example. My range of date is as follows: Mon 8 Aug - Sunday 14 Aug. Now my tasklist is as follows: item1: Date 9Aug, item2: Date 11Aug.
So my tempTask must be as follows: item1: Date 8 Aug, taskID: null, ID: null, item2: Date 9 Aug, taskID: 678, ID: 7, item3: Date 10Aug, taskID: null, ID: null, item4: Date11 Aug, taskID:890, ID: 34, item5: Date 12 Aug, taskID: null, ID: null, item6: Date 13 Aug, taskID: null, ID: null
Second example:
My range of date is as follows: Mon 8 Aug - Sunday 14 Aug. Now my tasklist is as follows: item1: Date 9Aug, item2: Date 11Aug, item3: Date 14Aug
So my tempTask must be as follows: item1: Date 8 Aug, taskID: null, ID: null, item2: Date 9 Aug, taskID: 678, ID: 7, item3: Date 10Aug, taskID: null, ID: null, item4: Date11 Aug, taskID:890, ID: 34, item5: Date 12 Aug, taskID: null, ID: null, item6: Date 13 Aug, taskID: null, ID: null, item4: Date14 Aug, taskID:894, ID: 74,
bool
for TimeIsPresent, and start it set tofalse
? – Paul BelloraTimeIsPresent
as abool
, you don't need the quotes aroundtrue
andfalse
-- in fact, you can useif (TimeIsPresent)
orif (!TimeIsPresent)
instead. – GabeEndDate
coming from and why do you keep re-calculatingEndDate.AddDays(1)
? – Romanwhile (EndDate.AddDays(1) != tempDate)
will not change the value ofEndDate
. – user47589