C# Linq 按 DateTime 分组忽略小时和分钟
我有一个 SortedList,其中 DateTime 作为键,int 作为值。 DateTime 值将附加小时和分钟。是否可以编写一个 C# linq 过程来按单个日期对 SortedList 进行分组,忽略小时并对值求和?
I have a SortedList that has DateTime for the key and int for the value. The DateTime value will have the hour and minute attached. Is it possible to write a C# linq procedure to group the SortedList by a single Date ignoring the hour and sum-up the value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 LINQ 就非常简单了:
With LINQ it is pretty straightforward:
通过单一日期?
简单的!
假设您已将变量
singleDate
声明为DateTime
来获取总和,以下是查询:我使用
singleDate.Date
来删除singleDate
可能具有的任何时间信息,但除此之外,这应该相当简单。SortedList
使用 LINQ 有点痛苦。你能告诉我你为什么使用它吗?By a single date?
Easy!
Assuming you have declared the variable
singleDate
as aDateTime
to get the sum of, here is the query:I've used
singleDate.Date
to drop any time information thatsingleDate
may have, but otherwise this should be fairly straight forward.SortedList
is a little painful to work with LINQ. Can you tell me why you are using it?