2012-07-28 3 views
0

나는이의 dictionary (사용 LINQ)를 얻고 싶은사전의 컬렉션의 그룹 키와 추가 값은

dim dic as new ienumerable(of dictionary(of integer, double))= _ 
{dictionary(of integer, double) = new { {1, 0.5}, {5, 0.75} } _ 
,dictionary(of integer, double) = new { {1, 0.3}, {4, 0.46} }} 

(즉석에서이 말이 희망 않았다)과 같이 사전의 컬렉션을 가지고 같은 동일한 키의 합이 너무

dictionary(of integer, double) = { {1, 0.8}, {4, 0.46}, {5, 0.75} } 

그래서, 즉, 나는 keysgroup 및 그룹화 된 항목의 values을 추가하고 싶습니다.

나는 쉽게 for loop으로 이것을 할 수 있었지만 나는 linq을 배우려고 노력 중이며 대신 그것을 사용하여 알아 내고 싶다. 아무도 나에게 거기에 도착하는 방법에 대한 약간의 암시를 줄 수 있었느냐?

답변

1
Dictionary<int, int> dict1 = new Dictionary<int, int>() {{1,9},{2,10},{3,11} }; 
     Dictionary<int, int> dict1 = new Dictionary<int, int>() {{1,9},{2,10},{3,11} }; 
     Dictionary<int, int> dict2 = new Dictionary<int, int>() { { 1, 9 }, { 2, 10 }, { 3, 11 } }; 
     Dictionary<int, int> dict3 = new Dictionary<int, int>() { { 1, 9 }, { 2, 10 }, { 3, 11 } }; 
     List<Dictionary<int, int>> list = new List<Dictionary<int, int>> {dict1,dict2,dict3 }; 
     Dictionary<int, int> dictionary = new Dictionary<int, int>(); 
     list.SelectMany(a => a).GroupBy(a => a.Key).ToList().ForEach(el=>dictionary.Add(el.Key,el.Sum(sm=>sm.Value))); 

VB.net에 대한 지식이 없지만 C#에 있습니다. 이것이 도움이되기를 바랍니다.