المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Hashsets and objects...



C# Programming
04-27-2009, 12:12 AM
Hi all,

I want to populate a Hashset with a series of objects then perfom operations such as intersects and excepts but not on the objects contained within the hashset as a whole, rather a peroperty of the object.

So for example:

Hashset allPeople = new Hashset(){
new Person { name="Tom", age=22, eyeColour="blue" },
new Person { name="James", age=32, eyeColour="brown" },
new Person { name="Mark", age=25, eyeColour="brown" }
};

A hashet of "Person" objects that I then wish to compare against another hashset. The catch is though that I dont want the objects to have to be the same. So another hashset:

Hashset brownEyedPeople = new Hashset(){
new Person { name="Jake", age=35, eyeColour="brown" },
new Person { name="Sam", age=44, eyeColour="brown" }
};

I then want to do an intersect so "allPeople.Intersect(brownEyedPeople)" but only on the property "eyeColour". So effectively it would remove James and Mark from the first hashset.

I hope that all made sense, I've been trying to find examples but havent been able to yet.

Cheers.