Search code examples
asp.netdistinctiequalitycomparer

Can't get Distinct and IEqualityComparer to work in ASP.net 6.1


I'm trying to remove duplicate entries when email address and phone number match. I've tried several solutions and I can't get anything to work. I've even tried the C# sample from the link posted below.

https://msdn.microsoft.com/en-us/library/bb338049(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

It seems that the line to call the actual method is not working correctly. In debug mode, I would expect the program to slow down a bit while it is executing the comparer. It seems to simply bypass the comparer. Here is how I'm calling the comparer.

IEnumerable<MerchantConsumer> noduplicates = consumers2.Distinct(new MerchantConsumerComparer2());

I've added the comparer code into my Controller and called it there. I've also added the comparer code into the actual Entity (MerchantConsumer class) and called it there.

Here is my comparer code.

 public class MerchantConsumerComparer2 : IEqualityComparer<MerchantConsumer>
    {
        // Products are equal if their names and product numbers are equal.
        public bool Equals(MerchantConsumer x, MerchantConsumer y)
        {

            //Check whether the compared objects reference the same data.
            if (Object.ReferenceEquals(x, y)) return true;

            //Check whether any of the compared objects is null.
            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;

            //Check whether the products' properties are equal.
            return x.Email == y.Email && x.PhoneNumber == y.PhoneNumber;
        }

        // If Equals() returns true for a pair of objects 
        // then GetHashCode() must return the same value for these objects.

        public int GetHashCode(MerchantConsumer consumer)
        {
            //Check whether the object is null
            if (Object.ReferenceEquals(consumer, null)) return 0;

            //Get hash code for the Name field if it is not null.
            int hashEmail = consumer.Email == null ? 0 : consumer.Email.GetHashCode();

            //Get hash code for the Code field.
            //int hashProductCode = product.Code.GetHashCode();
            int hashPhone = consumer.PhoneNumber == null ? 0 : consumer.PhoneNumber.GetHashCode();

            //Calculate the hash code for the product.
            return hashEmail ^ hashPhone;
        }

    }

This works, but it doesn't remove duplicate emails.

var consumersFiltered = consumerList.GroupBy(i => i.PhoneNumber).Select(g => g.FirstOrDefault()).ToList();

Are there any new methods of removing duplicates I need to look into?

Any help is much appreciated. Thanks!


Solution

  • I actually got this to work by converting it to a list like so. In all the examples I saw, none of them converted to a list.

    var noduplicates = consumers2.Distinct(new MerchantConsumerComparer2()).ToList();
    

    It seems to be pretty fast and it works whether the comparer is in the Controller or in the Entity.