Dictionary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    Dictionary

    Hello,

    I have the following Dictionary:

    var roles = new Dictionary<Role , string>();
    roles.Add(Role. Administrator, "Administrador" );
    roles.Add(Role. Collaborator, "Colaborado r");

    I have a variable role of type Role (enum).

    I want remove all items from my Dictionary with exception of the one
    which key is the same as in role variable.

    How can I do this?

    Thanks,
    Miguel
  • Family Tree Mike

    #2
    Re: Dictionary

    I would think the quickest way would be to clear out the dictionary and just
    add the one you want to keep back into it.

    "shapper" <mdmoura@gmail. comwrote in message
    news:e5913211-7602-472d-a535-9f334a33c046@m3 6g2000hse.googl egroups.com...
    Hello,
    >
    I have the following Dictionary:
    >
    var roles = new Dictionary<Role , string>();
    roles.Add(Role. Administrator, "Administrador" );
    roles.Add(Role. Collaborator, "Colaborado r");
    >
    I have a variable role of type Role (enum).
    >
    I want remove all items from my Dictionary with exception of the one
    which key is the same as in role variable.
    >
    How can I do this?
    >
    Thanks,
    Miguel

    Comment

    • shapper

      #3
      Re: Dictionary

      On Oct 3, 5:02 pm, "Family Tree Mike"
      <FamilyTreeM... @ThisOldHouse.c omwrote:
      I would think the quickest way would be to clear out the dictionary and just
      add the one you want to keep back into it.
      >
      "shapper" <mdmo...@gmail. comwrote in message
      >
      news:e5913211-7602-472d-a535-9f334a33c046@m3 6g2000hse.googl egroups.com...
      >
      Hello,
      >
      I have the following Dictionary:
      >
           var roles = new Dictionary<Role , string>();
           roles.Add(Role. Administrator, "Administrador" );
           roles.Add(Role. Collaborator, "Colaborado r");
      >
      I have a variable role of type Role (enum).
      >
      I want remove all items from my Dictionary with exception of the one
      which key is the same as in role variable.
      >
      How can I do this?
      >
      Thanks,
      Miguel
      Forget it ... I was trying to use Linq but no method was available ...

      I forgot to add using System.Linq and forgot that when I don't add it
      I don't get the options as I right!

      Thanks,
      Miguel

      Comment

      • Ignacio Machin ( .NET/ C# MVP )

        #4
        Re: Dictionary

        On Oct 3, 11:35 am, shapper <mdmo...@gmail. comwrote:
        Hello,
        >
        I have the following Dictionary:
        >
        var roles = new Dictionary<Role , string>();
        roles.Add(Role. Administrator, "Administrador" );
        roles.Add(Role. Collaborator, "Colaborado r");
        >
        I have a variable role of type Role (enum).
        >
        I want remove all items from my Dictionary with exception of the one
        which key is the same as in role variable.
        >
        How can I do this?
        >
        Thanks,
        Miguel
        Hi,

        You could create a list of all the keys in the dictionary and then
        delete them, comparing each with your value, if you find your value
        you do not remove t

        Comment

        Working...