0

Why do I get the following error in the following code?

Error:

CS1503 Argument 1: cannot convert from 'System.Collections.Generic.List<System.Collections.Generic.Dictionary<long, System.Collections.Generic.List>>' to 'System.Collections.Generic.IEnumerable<System.Collections.Generic.Dictionary<long, System.Collections.Generic.IEnumerable>>'

Code:

Dictionary<TKey, List<TValue>> ReduceToDictConcatDistinctValues<TKey, TValue>(IEnumerable<Dictionary<TKey, IEnumerable<TValue>>> context)
    => context.SelectMany(dict => dict)
        .GroupBy(kvp => kvp.Key)
        .ToDictionary(
            g => g.Key,
            g => g.SelectMany(kvp => kvp.Value).Distinct().ToList());
            
var entry1 = new Dictionary<long, List<long>>(){
    {0, new List<long>(){0,1}},
    {1, new List<long>(){0,1}}
};
    
var entry2 = new Dictionary<long, List<long>>(){
    {0, new List<long>(){2,1}},
    {1, new List<long>(){2,3}}
};

var input = new List<Dictionary<long, List<long>>>(){entry1, entry2};

ReduceToDictConcatDistinctValues<long, long>(input).Dump();

If the function parameter is declared as IEnumerable<Dictionary<TKey, List<TValue>>>, it works just fine. Why can the outer List be cast to IEnumerable but the inner List cannot?

I have tried to reproduce the same with a List of Lists, but in this case it can be cast to IEnumerable<IEnumerable<X> just fine.

Vinicius
  • 1,131
  • 2
  • 11
  • 25

0 Answers0