簡體   English   中英

如何轉換`字典 <string, Dictionary<string, List<MyCustomClass> &gt;&gt;`至`Dictionary <string, List<MyCustomClass> &gt;`

[英]How to convert `Dictionary<string, Dictionary<string, List<MyCustomClass>>>` to `Dictionary<string, List<MyCustomClass>> `

我試過了:

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"

endResultDictionary<string, List<MyCustomClass>>

tempResultDictionary<string, Dictionary<string, List<MyCustomClass>>>

怎么了

更新:

抱歉,我寫的endResultDictionary<string, List<MyCustomClass>> 而不是 Dictionary<string, Dictionary<string, List<MyCustomClass>>> (已更新)

實際上我想從Dictionary<string, Dictionary<string, List<MyCustomClass>>>提取Dictionary<string, List<MyCustomClass>> ,轉換類型,轉換

您的.SelectMany(wert => wert as List<MyCustomClass>)返回一個枚舉數,該枚舉數返回List<MyCustomClass>實例。 您試圖將該列表轉換為Dictionary<string, List<MyCustomClass>> 這是不可能的,因為那些類型是不可交換的。

您必須自己確定要尋找的轉換路徑。 可以據此創建字典,但是您必須自己提出一個密鑰(必須是唯一的):

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

我認為您不懂字典。 檢查以下示例:

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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM