How to see common elements between two arrays of objects

How to see common elements between two arrays of objects. My intersection returns nothing. Object is created from Linq class in SQL.

0


a source to share


4 answers


You need to override the Equals method of the object. You can find some guidelines on the Microsoft website.

I have provided an example below:



public override bool Equals(System.Object obj)
{
    if (obj != null && obj is MyClass)
    {
        MyClass obj2 = (MyClass)obj;
        return (obj2.ID == this.ID);
    }
}

      

If you don't override this method, any sort / intersect / compare will compare objects based on their reference; therefore, if two objects refer to the same memory spot, they are considered "equal".

0


a source


In Java atleat, if you don't override the .equals () operator, it will check for object equality (essentially using ==). This is probably why the intersection is emtpy.



+1


a source


Have you overridden the Equals method?

+1


a source


You will of course have to sort them.

0


a source







All Articles