score:1

Accepted answer

you can use selectmany to expand these kinds of structures:

var expanded = dictionary.selectmany(outer => 
    outer.value.select(inner => new { 
       outerkey = outer.key, 
       innerkey = inner.key, 
       value =inner.value 
    })
);

now you can do:

foreach (var item in expanded)
{
  console.writeline("{0},{1},{2}", item.outerkey, item.innerkey, item.value);
}

after that, if you want to extract the inner key / values combination, you can just do:

var byinnerkey = expanded.tolookup(item=> item.innerkey, item => item.value)

which is real easy to print using something like

foreach (var item in byinnerkey)
{
   string values = string.join(", ",item);
   console.writeline(item.key + ", " + values);
}

(live example)


note: this uses this overload of the string.join method that is only available in .net 4 and above.

for .net 3.5 and below you can either loop the values, with a string / stringbuilder, or cast them out to a strings array explicitly

string values = string.join(", ",item.select(d => d.tostring()).toarray());

(live example 3.5)

score:0

something is weird about this question.. and it doesn't make sense.. if you are using dictionary<double, dictionary<double,double>> then there would be a key1, innerdictkey, innerdictvalue

but you are referring to a dict1val1? which is the inner dictionary it self..

in anycase you could loop through the dictionary like this

foreach(var kv in outerdic){
console.writeline(kv.key + ',' + kv.value); // kv.value would be a dictionary<double, double>
console.writeline(kv.value[kv.key]); // this would print the inner value of inner dictionary
}

score:0

if i understand it, the structure looks like this:

dict[1.0]  ==> dictionary  [1.0] ==> 1.1
                           [1.1] ==> 1.3
                           [1.2] ==> 1.5

dict[2.0]  ==> dictionary  [1.0] ==> 1.2
                           [1.1] ==> 1.4
                           [1.2] ==> 1.6

then we just step through the first dictionary, and match it to the second dictionary.

 var dict2 = dict[2.0];

 var lstresults = dict[1.0].select(kvp=> string.format("{0},{1},{2}",
                                      kvp.key, kvp.value, dict2[kvp.key])
                           .tolist();

score:1

        var d1 = new dictionary<double, double> { { 1.0, 1.1 } };
        var d2 = new dictionary<double, double> { { 1.0, 1.2 } };

        var d3 = new dictionary<double, double> { { 1.1, 1.3 } };
        var d4 = new dictionary<double, double> { { 1.1, 1.4 } };

        var dict1 = new dictionary<double, dictionary<double, double>> { { 1.0, d1 }, { 2.0, d3 } };
        var dict2 = new dictionary<double, dictionary<double, double>>() { { 3.0, d2 }, { 4.0, d4 } };

        var keys = dict1.values.selectmany(dict => dict.keys.tolist());
        var collection = keys.select(key1 => new
        {
            key = key1,
            values = keys.selectmany(key =>
                dict1.values.where(k1 => k1.containskey(key1)).select(k1 => k1[key1]).union(
                dict2.values.where(k2 => k2.containskey(key1)).select(k2 => k2[key1]))
                ).distinct().tolist()
        }).tolist();


        foreach (var x in collection)
        {
            console.write(x.key + ": ");
            foreach (var y in x.values)
            {
                console.write(y + ",");
            }
            console.writeline();
        }

Related Query

More Query from same tag