You will need to project this onto a new dictionary, for example
Dictionary<int, List<User>> myDictionary = ...;
Dictionary<int, IEnumerable<User>> resultingDictionary = myDictionary.ToDictionary(kvp => kvp.Key, kvp => (IEnumerable<User>)kvp.Value)
You cannot cast from Dictionary<int, List<User>>to Dictionary<int, IEnumerable<User>>because, if you could, the following is possible:
Dictionary<int, List<User>> myDictionary = ...;
Dictionary<int, IEnumerable<User>> castDictionary = myDictionary;
castDictionary.Add(5, new HashSet<User>());
You can also look at covariance and contravariance on interfaces to see where the constraints lie.
source
share