简体   繁体   中英

cannot convert from Dictionary to IDictionary

I'm trying to create interface with method

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

then call this method like

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

I expected that it should works but got an error

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

Why I can't use interface IDictionary here? Should I cast manually?

Change your assigning type.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM