Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Hashsets and objects...

Hashsets and objects...

Scheduled Pinned Locked Moved C#
tutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    daviiie
    wrote on last edited by
    #1

    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.

    L 1 Reply Last reply
    0
    • D daviiie

      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.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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


      D 1 Reply Last reply
      0
      • L Luc Pattyn

        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


        D Offline
        D Offline
        daviiie
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • D daviiie

          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.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups