views:

111

answers:

4

How do I see common Items between 2 array of Objects. My intersect is not returning anything. The object is created from a Linq to SQL class.

+1  A: 

In Java atleat, unless you override the .equals() operator, it will test for object equality (essentially using ==). That might be why the intersection is emtpy.

Mike Pone
+1  A: 

Did you override the Equals method?

Kai Wang
A: 

You will have to sort them, of course.

ryansstack
A: 

You will need to override the object's Equals method. You can find some guidelines at Microsoft's web site.

I've provided a sample 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 do not override this method, any kind of sort/intersect/comparison will compare the objects based on their reference; so, if two objects refer to the same spot in memory, they are considered "equal."

Ed Altorfer
Thanks you very much.
Greens
My class definition is in the web service and I am trying to do the intersect in the client side. I overrided the Equals method still didnt work.
Greens
Also my intersect doesnt work.
Greens
Your comments don't really give me enough detail to provide you with a better solution. If you could provide an example of what you're doing, it would make for a more succinct question.
Ed Altorfer