Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Dan 1248 posts 3733 karma points admin c-trib
    Jul 18, 2018 @ 20:12

    Split collection of calendar event nodes into multiple objects based on start and end dates


    I have a website which lists calendar events. There's a master events document type and 2 different child document types: simpleEvent and rangeEvent.

    Simple events contain a startDate property only. Range events contain a startDate and endDate property.

    I can grab all events from the main events page and list them like this:

    var events = Model.Content.Children().OrderBy(x => x.GetPropertyValue<DateTime>("startDate"));
    foreach(var event in events){
        <p>@event.Name (start date: @event.GetPropertyValue("startDate"))</p>

    All good so far. However, what I want to do is separate range events so that they effectively have two entries in the calendar listing; one entry showing the start date and another entry showing the end date.

    So it might result in something like this:

    • Simple Event 1 (start date: 01/01/2018)
    • Simple Event 2 (start date: 02/01/2018)
    • Range Event 1 (start date: 03/01/2018)
    • Simple Event 3 (start date: 07/01/2018)
    • Range Event 1 (end date: 08/01/2018)
    • Simple Event 4 (start date: 10/01/2018)

    Note how Range Event 1 has two entries, one for its start date and one for its end date. I'm aware I could create 2 content nodes for this, one for range start and another for range end but I really don't want to do that.

    What I think needs to be done is something along the lines of:

    1. After declaring the events variable, create a new additional variable of only range events (e.g. var rangeEvents = events.Where(x => x.DocumentTypeAlias == "rangeEvent").OrderBy(x => x.GetPropertyValue<DateTime>("endDate"))

    2. Concatenate the events and rangeEvents together into a single collection.

    3. Somehow cast the start dates of the events collection and the end dates of the rangeEvents collection to a common variable, as the collection needs to be sorted on both startDate and endDate, depending on event type.

    I'm wondering if there's some dastardly LINQ query that can be done to achieve this, or do I literally have to create a custom class (e.g. a calendarItem object) and iterate over all of the events and rangeEvents objects, adding them to a new collection of calendarItems?

    If the latter, it should be fairly straight forward; I guess I'm asking if there's a neater way.

    Thanks folks :)

Please Sign in or register to post replies

Write your reply to: