Michael Sandler
Michael Sandler

Reputation: 1309

Adding a dictionary to a collection - just the values?

I am filling a combobox with dropdown values of type WagonType. The function I am calling returns a dictionary where the key is the WagonTypeID and the value is the WagonType.

RepositoryItemComboBox comboWagonTypes;
Dictionary<int, WagonType> GetAllWagonTypes()
{
  ...
}

If I use AddRange to fill the collection will it insert only the values or both the keys and the values?

comboWagonTypes.Items.AddRange(GetAllWagonTypes());

Or do I need to iterate through the dictionary and insert the values myself?

foreach (var wagonType in GetAllWagonTypes())
   comboWagonTypes.Items.Add(wagonType.Value)

Upvotes: 1

Views: 158

Answers (2)

Axel Kemper
Axel Kemper

Reputation: 11357

Just use the Values property. Dictionary has Keys and Values as properties. Give it a try!

Upvotes: 0

Ventsyslav Raikov
Ventsyslav Raikov

Reputation: 7212

You need to use the Values property of the dictionary.

Just do

comboWagonTypes.Items.AddRange(GetAllWagonTypes().Values);

Upvotes: 4

Related Questions