Hashsets and objects...
-
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.
-
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.
daviiie wrote:
I hope that all made sense,
Not quite, how can allPeople know it should drop all items with eyeColour="brown" if all you give it is a collection of people (brownEyedPeople)? The collection does not know what is common to all. Should it look for common properties? it might then drop all people that have an 'm' in their name?
allPeople.Intersect("eyeColour", "brown")
would make sense, and require reflection to do it. BTW: some people have eyes with unequal colours... :)Luc Pattyn [Forum Guidelines] [My Articles]
Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in
-
daviiie wrote:
I hope that all made sense,
Not quite, how can allPeople know it should drop all items with eyeColour="brown" if all you give it is a collection of people (brownEyedPeople)? The collection does not know what is common to all. Should it look for common properties? it might then drop all people that have an 'm' in their name?
allPeople.Intersect("eyeColour", "brown")
would make sense, and require reflection to do it. BTW: some people have eyes with unequal colours... :)Luc Pattyn [Forum Guidelines] [My Articles]
Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in
Ahh yes wasn't entirely clear sorry. Basically I want to do an intersect but only on object properties but retaining the original objects. Either that or can you suggest an a better way to extract objects from a hashset based on criterium? I'm looking to use hashsets better performance to reference objects in memory. Lists and Dictionarys arent doing it for me.
-
Ahh yes wasn't entirely clear sorry. Basically I want to do an intersect but only on object properties but retaining the original objects. Either that or can you suggest an a better way to extract objects from a hashset based on criterium? I'm looking to use hashsets better performance to reference objects in memory. Lists and Dictionarys arent doing it for me.
Hi, like this?
HashSet< Person> allPeople=new HashSet< Person>();
allPeople.Add(...);HashSet< Person> somePeople=new HashSet< Person>();
foreach(Person p in allPeople) if (p.EyeColour!="brown") somePeople.Add(p);BTW: I don't understand what you mean by "Lists and Dictionarys arent doing it for me." PS: someone is bound to react and say it is much simpler using LINQ.
Luc Pattyn [Forum Guidelines] [My Articles]
Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in