从IQueryable的字典字典

Glo*_*ups 5 .net c# iqueryable

我想要一个从IQueryable公式返回字典的方法.通常情况下,

Dictionary<int, Dictionary<DateTime, string>>
Run Code Online (Sandbox Code Playgroud)

起初,我看了ToDictionary()当然,却找不到一种方法来一个接一个地声明两个.

然后,我喜欢ToLookup(),我有一个方法来携带ILookup,字符串是我的辅助字典(DateTime.Tostring()+另一个字符串)...你跟着?:)但我没有发现ILookup解决方案真的很舒服(解析日期到字符串,当我收到数据 - >字符串到日期.. beuark)

那样会有类似的东西

return this.ttdc.Trackers.GroupBy(t => new { TrackerTaskId = t.TrackerTaskID, DateGenerated = t.TrackerDateGenerated })
                    .Select(t => new { taskId = t.Key.TrackerTaskId, DateGenerated = t.Key.DateGenerated, count = t.Count() })
                    .ToLookup(k => k.taskId.Value, k => k.DateGenerated.Value.ToString() + "|" + k.count);
Run Code Online (Sandbox Code Playgroud)

现在,我正在考虑创建一个自创的类列表,其中包含我需要的3个信息作为属性.

你能帮助我选择最好的模式吗?此方法托管在Windows服务上,我想限制传输到客户端的数据量.

谢谢

Met*_*ght 4

GroupBy这是一个使用and的示例ToDictionary

var testValues = new[]{new {ID = 1, Date = new DateTime(2010,1,1), Str = "Val1"},
                       new {ID = 1, Date = new DateTime(2011,2,2), Str = "Val2"},
                       new {ID = 2, Date = new DateTime(2010,1,1), Str = "Val3"}};
var dict = testValues.GroupBy(item => item.ID)
                     .ToDictionary(grp => grp.Key, 
                                   grp => grp.ToDictionary(item => item.Date, 
                                                           item => item.Str));
Run Code Online (Sandbox Code Playgroud)