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 Articles