c# - Getting key-value combinations from a Dictionary using LINQ -
let's suppose have following dictionary:
private dictionary<string, ienumerable<string>> dic = new dictionary<string, ienumerable<string>>(); //..... dic.add("abc", new string[] { "1", "2", "3" }); dic.add("def", new string[] { "-", "!", ")" }); how can ienumerable<tuple<string, string>> containing following combinations:
{ { "abc", "1" }, { "abc", "2" }, { "abc", "3" }, { "def", "-" }, { "def", "!" }, { "def", ")" } } it not have to tuple<string, string>, seemed more appropiate type.
i looking simple linq solution if there any.
i have tried following:
var comb = dic.select(i => i.value.select(v => tuple.create<string, string>(i.key, v))); but comb ends being of type ienumerable<ienumerable<tuple<string, string>>>.
you want enumerable.selectmany flatten out ienumerable<ienumerable<t>>:
var comb = dic.selectmany(i => i.value.select( v => tuple.create(i.key, v))); which yields:

Comments
Post a Comment