【问题标题】:Sum of Values in SortedList and Condition on KeysSortedList 中的值总和和键上的条件
【发布时间】:2016-11-06 05:36:30
【问题描述】:

我想用 linq 总结 SortedList<DateTime, double> 的某些值(时间范围从开始到结束)。键包含工作日的日期,值包含给定日期的可能工作时间数。我想回答的问题是,在给定的时间范围内可能有多少小时。

我设法计算了钥匙的数量,但我现在卡在了总数上。

计算键的代码(感谢stackoverflow)如下所示:

        double ats = (from n in DaysAndHours.Keys
                   where n >= start
                   where n <= end
                   select n).Count();

如何更改它,以使用日期范围内的值填充 ats

谢谢!

【问题讨论】:

标签: c# linq


【解决方案1】:

假设你想添加值后应用条件,然后使用以下代码并根据需要进行修改

double result = DaysAndHours.Where(n => n.Key >= start)
                            .Where(n => n.Key <= end)
                            .Sum(n => n.Value)

【讨论】:

  • 像魅力一样工作。谢谢!
猜你喜欢
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多