Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate key and value in dictionary<string,<IEnumerable string>> to make one list

I have

Dictionary<string,IEnumerable<string>> pathAndItems = new Dictionary<string,IEnumerable<String>>();

such as

this/is/path/: {hey, ho, lets, go}
another/path/: {hey, hello}

what I want to do is make one IEnumerable with all concatenaded values.

this/is/path/hey, this/is/path/ho, this/is/path/lets, this/is/path/go, another/path/hey, another/path/hello

I can get all in one, but how can I prepend the key to each?

var SL_requirements = SL_requirementsDict.SelectMany(kvp => kvp.Value);

edit: I would like to do it as a LINQ expression rather than loops

like image 315
Viktor Mellgren Avatar asked Jul 29 '13 10:07

Viktor Mellgren


1 Answers

There are various ways of skinning this. SelectMany lets you specify what to do with each (source, projected-element) pair too, either in a query expression or not:

var query = from pair in dictionary
            from value in pair.Value
            select pair.Key + "/" + value;

Or in dot notation:

var query = dictionary.SelectMany(kvp => kvp.Value,
                                  (kvp, value) => kvp.Key + "/" + value);
like image 66
Jon Skeet Avatar answered Oct 17 '22 13:10

Jon Skeet