Skip to content

OrderedDistinct() in Calendar.GetOccurrences() not working as expected #866

Description

@minichma

Follow up of the discussion in https://github.com/ical-org/ical.net/pull/864/files#r2359270375

Calendar.GetOccurrences() is expected not to return duplicate occurrences, so it uses OrderedDistinct() for removing duplicates. The operator is used instead of just .Distinct() for performance reasons because it leverages the assumed fact that the sequence of occurrences is ordered. Due to the order it assumes that duplicate values are always returned by the enumerable adjacent to each other but this is not necessarily the case. Instances of Occurrence are considered equal if both, the Period and the Source are equal. However, the sequence of occurrences is only ordered by Occurrence.Period.StartTime but not by the other properties that are used for determining equality.

In particular

  • Period.Equals compares all, a Period's Start, End and Duration but CompareTo only compares the StartTime property
  • Occurrence.Equals compares the Period and Source property using Object.Equals while CompareTo only compares the Period and therefore only the Period.StartTime property.

(also compare this discussion https://github.com/orgs/ical-org/discussions/606#discussioncomment-11867080)

IMHO it is not clear what the best is to solve this issue. I see two main approaches:

  1. Fix Equals vs CompareTo so they are consistent (i.e. equality of Equals matches that of CompareTo() == 0
  2. Let OrderedDistinct() deal with non-perfect ordering.

While having a consistent implementation of Equals and CompareTo, this might not always be possible (as discussed here), so probably 1) will not be sufficient and 2) will be needed.

Ical.Net 5.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions