简体   繁体   中英

How to convert `Dictionary<string, Dictionary<string, List<MyCustomClass>>>` to `Dictionary<string, List<MyCustomClass>> `

I tried:

endResult = tempResult.Where(x => x.Key.Equals(nameOfMyList))
                      .SelectMany(wert => wert as List<MyCustomClass>)
                      .Cast<Dictionary<string, List<MyCustomClass>>>().ToDictionary(); //error "can not convert type"

endResult is Dictionary<string, List<MyCustomClass>> .

tempResult is Dictionary<string, Dictionary<string, List<MyCustomClass>>> .

What's wrong here?

UPDATED:

Sorry, I wrote endResult is Dictionary<string, List<MyCustomClass>> and not Dictionary<string, Dictionary<string, List<MyCustomClass>>> (updated it)

Actually I want to extract Dictionary<string, List<MyCustomClass>> from Dictionary<string, Dictionary<string, List<MyCustomClass>>> , kind of conversion, cast

Your .SelectMany(wert => wert as List<MyCustomClass>) returns an enumerator returning instances of List<MyCustomClass> . You are trying to cast that list to Dictionary<string, List<MyCustomClass>> . That is not possible, since those types are not exchangeable.

You have to make sure for yourself what conversion path you are looking for. It is possible to create a dictionary out of this, but you have to come up with a key yourself (which have to be unique):

.SelectMany(wert => wert as List<MyCustomClass>)
.ToDictionary( k => whatEverYourKeyIs
             , v => v /* this is the list */
             )

I think you don't understand dictionaries. Check this example:

var foo = new Dictionary<string, Dictionary<string, List<MyCustomClass>>>();
...
// Add some content to foo.
...

string nameOfMyList = ""; // Something that exists as a key in foo.

Dictionary<string, List<MyCustomClass>> result = foo[nameOfMyList];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM