c# - 无法从 Dictionary 转换为 IDictionary

标签 c# dictionary

我正在尝试使用方法创建界面

void Import(int id, IDictionary<int, IDictionary<int, string>> items);

然后像这样调用这个方法

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

我预计它应该可以工作,但出现错误

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>>'  

为什么我这里不能使用接口(interface)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));

关于c# - 无法从 Dictionary 转换为 IDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49325505/

相关文章:

python - 如何使用字典为每个学生存储多个条目

c# - 如何在 .net core 3.0 中使用 System.Text.Json 反序列化部分 json?

c# - ConfigurationChangeWatcher.Poller()

C# 任务 - 我是否遇到句柄数量的可疑增加

python - 反转/反转字典映射

python - Python 3.6 和 Python 3.5 中字典顺序的区别

ios - 'NSInternalInconsistencyException',原因 : 'attempt to insert row 4 into section 0, but there are only 4 rows in section 0 after the update'

c# - 让一种方法等待特定事件完成

c# - 如何使用 LINQ 查询不包含来自另一个列表的子字符串条目的字符串列表

python - 连接字典