Flatten a C# Dictionary of Lists with Linq

Very easily:

var list = dictionary.Values              // To get just the List<string>s
                     .SelectMany(x => x)  // Flatten
                     .ToList();           // Listify

Here the SelectMany call takes a sequence of inputs (the lists which make the values of the dictionary) and projects each single input into another sequence of outputs – in this case “the elements of the list”. It then flattens that sequence of sequences into a single sequence.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)