Merging Dictonaries in C# using LINQ

前端 未结 2 1205
闹比i
闹比i 2021-01-15 03:36

i\'ve three Dictonaries like

Dictionary> D1 = new Dictionary>(); 
Dictionary

        
2条回答
  •  迷失自我
    2021-01-15 03:55

    Something like this (maybe needs optimisation)?

         var lr =
        (from gr in
            (from pair in D1.Union(D2).Union(D3)
             group pair by pair.Key)
         select new KeyValuePair>>(gr.Key, gr.Select(x => x.Value))
        ).ToDictionary(k => k.Key, v => v.Value.Aggregate((t, s) => (new List(t.Union(s)))));
    

提交回复
热议问题