Frage

I have something like this:

private IDictionary<A, IDictionary<B, C>> data;

and I want to do something like:

IDictionary<B, C> values = new Dictionary<B, C>();
values = Data.Values;

like would I do in java, but this isn't working. I can't figure it out. Thanks for help

error:

Cannot implicitly convert type ICollection> to IDictionary

War es hilfreich?

Lösung 2

If you are not sure what will come out, try to use type-inference

var coll = data.Values;

Then try to acces the Collection using a foreach-loop to access individual dictionaries.

foreach(var dic in coll){
//work on Dictionary
}

See here for a reference.

Andere Tipps

You would still call .Values, but it returns a ValueCollection, not a list. To get just the values, and not a list of Key/Value pairs, use a Select:

List<C> values = data.Values.Select(x => x.value);

http://msdn.microsoft.com/en-us/library/ekcfxy3x.aspx

You are trying to access an ICollection of your IDictionary<B, C> which will not work since there any many Dictionaries in the Collection.

If you wish to map all the Dictionaries down then you can use a Lookup:

IDictionary<A, IDictionary<B, C>> data;
var values = data.ToLookup(d => d.Value.SelectMany(d => d.Key), d => d.Value.SelectMany(d => d.Value));

Which will give you multiple keys to multiple values.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top