我正在尝试使用方法创建界面
void Import(int id, IDictionary<int, IDictionary<int, string>> items);
Run Code Online (Sandbox Code Playgroud)
然后像这样调用这个方法
Dictionary<int, Dictionary<int, string>> items = viewModel.Items
.GroupBy(t => t.Number)
.ToDictionary(t => t.Key, t => t.ToDictionary(x => x.LanguageId, x => x.Translation));
Import(id, items);
Run Code Online (Sandbox Code Playgroud)
我预计它应该有效,但出现错误
cannot convert from 'System.Collections.Generic.Dictionary<int, System.Collections.Generic.Dictionary<int, string>>' to 'System.Collections.Generic.IDictionary<int, System.Collections.Generic.IDictionary<int, string>>'
Run Code Online (Sandbox Code Playgroud)
为什么我不能在这里使用接口 IDictionary ?我应该手动投射吗?
更改您的分配类型。
Dictionary<int, IDictionary<int, string>> items = viewModel.Items
.GroupBy(t => t.Number)
.ToDictionary(t => t.Key, t => (IDictionary<int, string>) t.ToDictionary(x => x.LanguageId, x => x.Translation));
Run Code Online (Sandbox Code Playgroud)