Parse internal double dictionary using LINQ?

I have Dictionary<double, Dictionary<double,double>>where the keys of the internal dictionaries are the same, and I want to parse this so that the output is:

key1,dict1val1,dict2val1
key2,dict1val2,dict2val2
...

Sorry, I had to say that foreign keys are not needed.

Here is an example of what I do after:

(1.0, (1.0, 1.1)),(2.0, (1.0,1.2))
(1.0, (1.1, 1.3)),(2.0, (1.1,1.4))
(1.0, (1.2, 1.5)),(2.0, (1.2,1.6))

should produce the result:

1.0,1.1,1.2
1.1,1.3,1.4
1.2,1.5,1.6

Is there an elegant way to do this in LINQ? I saw similar questions for string dictionaries, but the syntax does not work for doubling.

I also ordered a book on this topic, but I need to do this as soon as possible.

+4
source share
4 answers

You can use SelectManyto expand such 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);
}

, /, :

var byInnerKey = expanded.ToLookup(item=> item.InnerKey, item => item.Value)

, -

foreach (var item in byInnerKey)
{
   string values = string.Join(", ",item);
   Console.WriteLine(item.Key + ", " + values);
}

( )


: string.Join, .net 4 .

.net 3.5 string/StringBuilder,

string values = string.Join(", ",item.Select(d => d.ToString()).ToArray());

( 3.5)

+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();
        }
+1

- .. . Dictionary<double, Dictionary<double,double>> key1, innerdictkey, innerdictvalue

dict1val1? .

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
}
0

, :

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

.

 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();
0

All Articles