1

I've been tasked with taking a legacy project written with WPF, that was integrated into a bigger application and making it stand-alone. It hasn't been touched for some time, and from experience, this will always lead to issues when trying to bring the associated packages up to date. My team regularly do maintenance to keep technologies up to date, but the codebase was maintained by a different team, so they haven't followed any maintenenace schedules. I'm finding an issue with ClosedXml whereby part of our code references an extension method HasDuplicates() (found in ClosedXml.Excel) that was available in 0.74.0 but when upgrading the the latest version (0.95.4) isn't present. Searching for help isn't returning anything useful, but am sure somewhere there must be a release note that states it has been removed or renamed but I can't find it!
Using 0.74.0, this code compiles:

if (serializedPeriodAllocations.Select(o => o.Period).HasDuplicates())
        {
            throw new Exception("Duplicate period allocations found!");
        }

But when I upgrade ClosedXml to 0.95.4 or above, it doesn't and I get the following error:

CS1061  'IEnumerable<int>' does not contain a definition for 'HasDuplicates' and no accessible extension method 'HasDuplicates' accepting a first argument of type 'IEnumerable<int>' could be found (are you missing a using directive or an assembly reference?)

Does anyone know if there is any documentation that will help me figure out why it no longer works, and if it's possible to upgrade?

MartinS
  • 111
  • 1
  • 14
  • Such a method appears to still exist: https://github.com/ClosedXML/ClosedXML/blob/develop/ClosedXML/Extensions/EnumerableExtensions.cs so I guess you just need to fix some `using`s or something – AakashM Sep 08 '21 at 14:19
  • Thanks @AakashM, appreciate the link. Looks like it's been moved from Excel to Extensions, am sure I tried that, but will try again. Thanks again – MartinS Sep 08 '21 at 15:17
  • 1
    I can't figure out how to implement it - it's not available in Excel or Extensions. I managed to retain the access to the extension by upgrading to 0.90.0, but any further and the issue returns. The main priority is to get the application running stand-alone and that's been achieved, so will deal with this issue later. – MartinS Sep 09 '21 at 07:05

1 Answers1

0

After further investigation, I found an Extensions class within the project that contained a HasDuplicates extension, which was coded like this:

    public static bool HasDuplicates<T>(this ICollection<T> source)
    {
        return source.Distinct().Count() != source.Count;
    }

I added in an override for the extension that accepted IEnumerable:

    public static bool HasDuplicates<T>(this IEnumerable<T> source)
    {
        var enumerable = source as T[] ?? source.ToArray();
        return enumerable.Distinct().Count() != enumerable.Length;
    }

After removing the using for ClosedXml.Excel, the HasDuplicates() doesn't return an error, as expected.

MartinS
  • 111
  • 1
  • 14