gpt4 book ai didi

c# - 如何按日期时间对字典进行分组并对 int 求和

转载 作者:行者123 更新时间:2023-11-30 12:30:36 27 4
gpt4 key购买 nike

我有这段代码:

    var tradeReqsBySegment = segGroups.Join(pPeriods, s => s.MarketSegmentId, p => p.EntityId, (s, p) => new
{
SegmentCode = s.SegmentCode, // string
Time = p.StartLocal, // datetime
TradeRequirement = p.Volume // double
})
.GroupBy(s => s.SegmentCode)
.ToDictionary(g => g.Key, g => g.ToDictionary(i => i.Time, i=>i.TradeRequirement));

我希望 g.ToDictionary(i => i.Time, i=>i.TradeRequirement)) 按时间分组,TradeRequirement求和或平均。我该如何处理?

此外,是否可以按月对时间进行分组,例如 get :

Time - TradeReq
01/2013 - 500
02/2013 - 234
...

最佳答案

g.GroupBy(gr => new DateTime(gr.Time.Year, gr.Time.Month, 1))
.ToDictionary(i => i.Key, i => i.Sum(s => s.TradeRequirement));

关于c# - 如何按日期时间对字典进行分组并对 int 求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15837982/

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