Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Calculating which recurring events happen in a date rang
Message
De
19/06/2010 04:25:04
 
 
À
18/06/2010 09:42:52
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
LINQ
Divers
Thread ID:
01469538
Message ID:
01469719
Vues:
29
>>>>I was going to do some ugly processing with loops, but I figured maybe Linq could help me out.
>>>>
>>>>RecurringEvents
>>>>ID (PK)
>>>>StartDate (datetime)
>>>>RecurrenceDays (int)
>>>>
>>>>How can I calculate which of these recurring events happen within a specified date range. Linq's gotta have some sexy method that will do this easily...
>>>
>>>Linq would surely have nice ways to have it but your layout seems very inadequate to me. For example what does RecurrenceDays stand for? Assuming it is the days that the event would be available continously from start:
>>>
>>>
var evList = from e in events
>>>  where e.StartDate <= end && e.StartDate.AddDays( e.RecurrenceDays ) >= start
>>>  select e;
>>>
>>>In method syntax:
>>>
>>>
var evList = events.Where( e => e.StartDate <= end && e.StartDate.AddDays( e.RecurrenceDays ) >= start);
>>>
>>>PS: You could also create extension methods in your say "ScheduleExtensions" namespace and simply use those methods for selection. It then might look like (assuming you created WithIn extension method for RecurringEvent that takes start, end parameters):
>>>
>>>
var evList = events.Where( e => e.WithIn( start, end ));
>>>
>>>Is there a setting somewhere to say that this is C# code and not VFP? Maybe class attribute or another tag?
>>>Cetin
>>
>>I think RecurrenceDays indicates the number of days between occurences. So an event might occur, say, every 20 days. Then the requirement is not to find whether the *first* occurence falls between the start and end date but *any* occurrence (ie 20,40,60,80 days etc...)
>
>Correct.

Maybe something like:
    class Program
    {
        static void Main(string[] args)
        {
            List<RecurringEvent> events = new List<RecurringEvent>();

            events.Add(new RecurringEvent { Id = 1, StartDate = new DateTime(2010, 6, 19), RecurrenceDays = 20 });
            events.Add(new RecurringEvent { Id = 2, StartDate = new DateTime(2010, 6, 19), RecurrenceDays = 60});
            events.Add(new RecurringEvent { Id = 3, StartDate = new DateTime(2010, 6, 19), RecurrenceDays = 15 });
            events.Add(new RecurringEvent { Id = 4, StartDate = new DateTime(2010, 6, 19), RecurrenceDays = 1000 });

            DateTime rangeStart = new DateTime(2010, 12, 20);
            DateTime rangeEnd = new DateTime(2011, 01, 05);

            var v = from x in events
                    where IsInRange(x, rangeStart, rangeEnd)
                    select x.Id;
        }

        static bool IsInRange(RecurringEvent re, DateTime rangeStart, DateTime rangeEnd)
        {
            int daysToStart = (rangeStart - re.StartDate).Days;
            var firstAfterStart = (((int)(daysToStart / re.RecurrenceDays)) + 1) * re.RecurrenceDays;
            return re.StartDate.AddDays(firstAfterStart) <= rangeEnd;
        }
    }

    public class RecurringEvent
    {
        public int Id { get; set; }
        public DateTime StartDate { get; set; }
        public int RecurrenceDays { get; set; }
    }
Disclaimer: Don't know if the IsInRange() really works as expected - but you should get the idea.....
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform