gpt4 book ai didi

c# - 来自 IQueryable 的 Dictionary of Dictionary

转载 作者:太空狗 更新时间:2023-10-29 23:25:26 28 4
gpt4 key购买 nike

我想要一种从 IQueryable 公式返回 Dictionary of Dictionary 的方法。通常,

Dictionary<int, Dictionary<DateTime, string>>

起初,我当然查看了 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);

现在,我正在考虑使用我需要的 3 个信息作为属性来创建一个自创建类的列表。

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

谢谢

最佳答案

这是一个示例,使用 GroupByToDictionary:

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));

关于c# - 来自 IQueryable 的 Dictionary of Dictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9569094/

28 4 0