2016-05-09 194 views
0

我想:如何转换`词典<字符串,字典<字符串,列表<MyCustomClass >>>``到词典<字符串,列表<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>>,转换种类,投

+0

大概只是一个错字,但根据你的话题,我认为你的“endresult”类型应该是Dictionary >。 –

+0

已更新我的帖子 – kkkk00999

回答

0

我想你不明白的字典。检查这个例子:

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

您的.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 */ 
      ) 
+0

已更新我的帖子 – kkkk00999

相关问题