2012-04-16 45 views
0

我有以下對象:如何將字典詞典拼湊起來並用LINQ對內部詞典的值進行求和?

countDictionary As Dictionary(of Category, Dictionary(of Date, Integer)) 

Class有一個枚舉屬性。爲了演示的目的,我將其稱爲MasterCategory

我一直在試圖擺脫的對象,如下所示:

groupedCountDictionary As Dictionary(of MasterCategory, Dictionary(of Date, Integer) 

我能得到的最好結果是:

Lookup(of MasterCategory, Dictionary(of Date, Integer)) 

來源:

countDictionary.ToLookup(Function(o) o.Key.MasterCategory, Function(o) o.Value) 

對於每個MasterCategory值,結果爲IEnumerable (Of Dictionary(of Date, Integer))

但是,我需要IEnumerable的字典扁平化爲一個字典,每個日期的所有整數總和爲(總計數)。

然後,我試圖使用各種各樣的select s和group by s(從許多stackoverflow帖子)「扁平」它,但我的努力已經不足。

任何人都可以提出一種方法來做到這一點?尋找VB.Net如果可能的話。

編輯:

目前代碼

[Category Class] 
- MasterCategory As Enum 
- Name As String etc 

[countDictionary As Dictionary(of Category Objects, Dictionary(of Date, Integer))] 
- length 8 
- Children of 8 Categories: 
     3 with MasterCategory A, 1097 int's by date 
     4 with MasterCategory B, 1097 int's by date 
     1 with MasterCategory C, 1097 int's by date 

盡力而爲

[Lookup(of MasterCategory, Dictionary(of Date, Integer)] 
- length 3 
- Children of 3 Master Categories: 
    1 of IEnumerable(of Dictionary(of Date, Integer)) and length 3 
     3 x Dictionary length 1097 int's by date 

    1 of IEnumerable(of Dictionary(of Date, Integer)) and length 4 
     3 x Dictionary length 1097 int's by date 

    1 of IEnumerable(of Dictionary(of Date, Integer)) and length 1 
     3 x Dictionary length 1097 int's by date 

要求

[Dictionary(MasterCategory, Dictionary(of Date, Integer))] 
- length 3 
- Children of 3 Master Categories: 
    3 of Dictionary(of Date, Integer) with summed total int's 

回答

1

您是否在SelectMany之後?如果是這樣,這可能做你以後:

C#

test.SelectMany(x => x.Key.MasterCategory).Sum(x => x.ThePropertyToSum) 

其中在VB,我認爲是:

test.SelectMany(Function(x) x.Key.MasterCategory).Sum(Function(x) x.ThePropertyToSum) 
+0

謝謝 - 我在上面嘗試這是你寫它,但得到一個重載解析失敗的消息(不能訪問「SelectMany」可以用這些參數調用)。我無法測試Sum作爲選擇許多錯誤。我錯過了什麼嗎? 這是完整的錯誤:http://pastebin.com/pZizyyuy – 2012-04-16 14:09:30

+0

你可以更新你的問題與剝離下來,但完整的對象結構?這可能是我錯過了解你的問題。 – ilivewithian 2012-04-16 14:21:09

+0

已更新示例對象。對不起,我原本應該這樣做! :) – 2012-04-16 14:38:51