IEqualityComparer in Union not comparing expected values

64 views Asked by At

I'm trying to "fill" gaps in a set of time data with NAN for graphing purposes. I'm attempting to do this by creating a set of NAN data for all the possible time-periods and using a Union to merge them together. I'm hoping to have the real data overwrite the NAN values, leaving the data-set I want.

Here's my code to set up the NAN data-set & overwrite (with the previously obtained AveragedData):

        //Calculate time period
        var days = (int)(endDate - startDate).TotalMinutes + 1;

        //create a set of minute data for the correct time-range
        var clockQuery = from offset in Enumerable.Range(0, days)
                         select TimeSpan.FromMinutes(
        var completeSequence = clockQuery.Select(add => minRoundStartDate.Add(add));

        //Create the NAN dataset
        var nullData = from date in completeSequence
                                     select new SensorValue{ MeasureTime = date, Value = double.NaN };

        //Merge data
        var mergedData = nullData.Union(AveragedData, new SensorValueComparer()).ToList();

And here's the SensorValueComparer. At first I just had "compare" and tested for 0, but it didn't hit any values so I tested adding a point to break when both dates are simply the same (with minute data this should happen a lot). But that breakpoint doesn't get hit either. I've checked and mergedData contains both data sets, and occasions with MeasuredData that are equal.

    public class SensorValueComparer : IEqualityComparer<SensorValue>
    {
        public bool Equals(SensorValue x, SensorValue y)
        {
            var ret = x.MeasureTime.CompareTo(y.MeasureTime);
            if (x.MeasureTime.ToOADate() == y.MeasureTime.ToOADate())// && x.MeasureTime.Hour == y.MeasureTime.Hour && x.MeasureTime.Minute == y.MeasureTime.Minute)
            {
                System.Console.WriteLine("Test");
            }
            return ret == 0;
        }

        public int GetHashCode(SensorValue obj)
        {
            return obj.MeasureTime.GetHashCode();
        }
    }

Can anyone help with why this Union function isn't properly merging duplicates? Am I using it incorrectly?

Cheers

1

There are 1 answers

0
Joe On

I solved the issue by using a Linq statement instead of the Union. I had to re-group by as the previous method didn't produce times exact to the minuite:

        //merge Data
        var mergedData = (nullData.Concat(AveragedData)
                                 .GroupBy(x => {
                                        return new DateTime(x.MeasureTime.Year, x.MeasureTime.Month, x.MeasureTime.Day,
                                            x.MeasureTime.Hour, x.MeasureTime.Minute, 0);
                                    })
                                 .Select(g => new SensorValue
                                 {
                                     MeasureTime = g.Key,
                                     Value = g.Max(x => x.Value)
                                 }).OrderBy(x => x.MeasureTime)).ToList();