Dictionary

S

shapper

Hello,

I have the following Dictionary:

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

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
 
F

Family Tree Mike

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.
 
S

shapper

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.

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
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello,

I have the following Dictionary:

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Add item at start 5
Generics 7
Confused about generics ... 1
List<String> 15
Intersection of Lists 5
Sring[] to List<MyEnumeration> 5
Fill Dictionary in a Fluent Way 4
List. Add, Remove. 1

Top