![](/img/trans.png)
[英]convert List<Dictionary<string, string>> to Dictionary<string, Dictionary<string, string>>
[英]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"
endResult
是Dictionary<string, List<MyCustomClass>>
。
tempResult
是Dictionary<string, Dictionary<string, List<MyCustomClass>>>
。
怎么了
更新:
抱歉,我寫的endResult
是Dictionary<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.