2012-04-16 53 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