如何将`Dictionary <string,dictionary <string,=“”list <mycustomclass =“”>&gt;&gt;`转换为`Dictionary <string,list <mycustomclass =“”>&gt; `

时间:2016-05-09 08:48:41

标签: c# linq lambda casting

我试过了:

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, List<MyCustomClass>>中提取Dictionary<string, Dictionary<string, List<MyCustomClass>>>,转换类型,广告

2 个答案:

答案 0 :(得分: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 */
             )

答案 1 :(得分: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];