gpt4 book ai didi

c# - 嵌套字典 : group by date and then group by Enum value and summarize the count

转载 作者:行者123 更新时间:2023-11-30 20:28:04 25 4
gpt4 key购买 nike

我的原始数据是这样的:

IDictionary<DateTime, IDictionary<ReportType, int>> rawData

例子:

19/09/2017, [(ReportType.Open, 2)] 
25/09/2017, [(ReportType.Open, 15), (ReportType.Sent, 15)]
25/10/2017, [(ReportType.Open, 27), (ReportType.Sent, 15)]
29/10/2017, [(ReportType.Sent, 150)]

我正在努力实现以下结果:

data for open:
september-2017: 17
october-2017: 27

data for sent:
september-2017: 15
october-2017: 165
-----------------------------

我试过

var openData = rawData
.Select(g => new {
dt = string.Format("{0}/{1}", g.Key.Month, g.Key.Year),
g.Key.Year,
g.Key.Month,
action = g.Value.FirstOrDefault().Key,
total = g.Value.Values.Sum()
})
.GroupBy(l => new {
l.Month,
l.Year,
action = l.action
})
.Where(a => a.Key.action == ReportType.Open)
.OrderByDescending(a => a.Key)
.ToList();

我做错了什么?

打印部分(series1 派生自 Series 类):

foreach (var item in openData)
{
series1.Points.Add(new DataPoint()
{
YValues = new double[] { item.total },
AxisLabel = item.dt.ToString(),
ToolTip = item.total.ToString()
});
}

最佳答案

您过滤Open reporttype错误的地方;

            var openData = rawData
.Select(g => new {
dt = string.Format("{0}/{1}", g.Key.Month, g.Key.Year),
g.Key.Year,
g.Key.Month,
actions = g.Value.Where(x => x.Key == ReportType.Open) //Filter it here
})
.GroupBy(l => new {
l.Month,
l.Year
})
.Select(s =>
new
{
s.Key.Month,
s.Key.Year,
s.Sum(x => x.actions.Sum(sum => sum.Value))
})
.ToList();

关于c# - 嵌套字典 : group by date and then group by Enum value and summarize the count,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47862975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com