Hi.
originally the "solution" had a singular class which had:
DateFrom
DateTo
StartAt
EndAt
properties. all DateTime values
constructor:
Thats fine.Code:ID = Guid.Empty; Start = StringUtils.GetBaseTime().Add(new TimeSpan(0, 0, 1)); Finish = StringUtils.GetBaseTime().Add(new TimeSpan(23, 59, 59)); FinishDate = StringUtils.GetBaseTime().Add(new TimeSpan(18250, 0, 0, 0)); StartDate = StringUtils.GetBaseTime().Add(new TimeSpan(0, 0, 1));
There is also a method called "IsValid()" which does:
thats fine.Code:public bool IsValid() { DateTime now = DateTime.Now; if (now.TimeOfDay < Start.TimeOfDay) return false; if (now.TimeOfDay > Finish.TimeOfDay) return false; var logicalDay = EndOfDayHelper.GetLogicalDay(now); if (logicalDay.Start < StartDate) return false; if (logicalDay.Start > FinishDate) return false; switch (logicalDay.Start.DayOfWeek) { case DayOfWeek.Sunday: return (Sunday); case DayOfWeek.Monday: return (Monday); case DayOfWeek.Tuesday: return (Tuesday); case DayOfWeek.Wednesday: return (Wednesday); case DayOfWeek.Thursday: return (Thursday); case DayOfWeek.Friday: return (Friday); case DayOfWeek.Saturday: return (Saturday); } return true; }
but now, there is a requirement to be able to hold a collection of these dates/times. Thats ok, ive created a collection to do so.
Now we need to do the same IsValid() validation but only selecting a singular item, making sure that the item in question from the collection is suited for the present time.
my query seems to work but i am not entirely sure how to handle it for the scenario of Dates (with times) only, so the same logic as above is applied.
please ignore the duplicate code for now.
I hope it makes sense, but understand the confusion/understanding of my explanation!Code:public bool IsValid() { DateTime now = DateTime.Now; var logicalDay = EndOfDayHelper.GetLogicalDay(now); var validityPeriodsQueryable = Enumerable.Cast<ValidityPeriodItem>(ValidityPeriods); if (validityPeriodsQueryable.Count() == 0) { switch (logicalDay.Start.DayOfWeek) { case DayOfWeek.Sunday: return (Sunday); case DayOfWeek.Monday: return (Monday); case DayOfWeek.Tuesday: return (Tuesday); case DayOfWeek.Wednesday: return (Wednesday); case DayOfWeek.Thursday: return (Thursday); case DayOfWeek.Friday: return (Friday); case DayOfWeek.Saturday: return (Saturday); } return true; } var tmpEligibleItem = (from ValidityPeriodItem currentItem in validityPeriodsQueryable where now.TimeOfDay > currentItem.Start.TimeOfDay && now.TimeOfDay < currentItem.Finish.TimeOfDay orderby currentItem.Start ascending // always the most present/current one. NOT in the future. select currentItem ); var eligibleItem = tmpEligibleItem.FirstOrDefault(); if (eligibleItem != null) // we have rules to apply { if (eligibleItem.AllowUsage.HasValue && eligibleItem.AllowUsage.Value == false) { return false; } if (now.TimeOfDay < eligibleItem.Start.TimeOfDay) return false; if (now.TimeOfDay > eligibleItem.Finish.TimeOfDay) return false; if (logicalDay.Start < eligibleItem.StartDate) return false; if (logicalDay.Start > eligibleItem.FinishDate) return false; switch (logicalDay.Start.DayOfWeek) { case DayOfWeek.Sunday: return (Sunday); case DayOfWeek.Monday: return (Monday); case DayOfWeek.Tuesday: return (Tuesday); case DayOfWeek.Wednesday: return (Wednesday); case DayOfWeek.Thursday: return (Thursday); case DayOfWeek.Friday: return (Friday); case DayOfWeek.Saturday: return (Saturday); } return true; } } else { /*.... old code as above.... */} }
basically, the same logic should apply as "before". but because we are now dealing with a collection, I only ever want to get a singular (if applicable) entity out from the collection and perform the logic based on that entity. but the query I am using, not sure if it is the correct query. I know it works for the time of day but im sure I can improve that some how, and also fix any issues which involves dates.





any chance of a regain? 
Reply With Quote