如何编写以下 group by 和剥离时间 Linq 查询?
我有这样的列表
Id Date
1 01/03/2011 14:15:23.320
1 01/03/2011 16:15:23.320
1 01/03/2011 18:15:23.320
1 01/04/2011 19:15:23.320
2 01/03/2011 14:15:23.320
2 01/03/2011 15:15:23.320
2 01/03/2011 18:15:23.320
2 01/05/2011 19:15:23.320
2 01/07/2011 20:15:23.320
- 我想要的输出是 -
Id Date
1 01/03/2011
1 01/04/2011
2 01/03/2011
2 01/05/2011
2 01/07/2011
那么如何在 id 的第一个列表上进行分组并仅采用日期部分并在 Linq 中实现上述结果?
I have list like this-
Id Date
1 01/03/2011 14:15:23.320
1 01/03/2011 16:15:23.320
1 01/03/2011 18:15:23.320
1 01/04/2011 19:15:23.320
2 01/03/2011 14:15:23.320
2 01/03/2011 15:15:23.320
2 01/03/2011 18:15:23.320
2 01/05/2011 19:15:23.320
2 01/07/2011 20:15:23.320
My desired output is -
Id Date
1 01/03/2011
1 01/04/2011
2 01/03/2011
2 01/05/2011
2 01/07/2011
So how can I group by on the first list on id and take only the date part and achieve the results above in Linq ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样的操作:
基本上,
DateTime.Date
会去除时间信息。要循环结果,您可以这样做:
Try something like this:
Basically,
DateTime.Date
strips the time information.To loop through the result, you could do:
您似乎想要根据日期和 id 进行分组,因此您希望将它们提取到可以分组的结构中。当您创建此键结构时,您可以使用 Date 属性设置日期格式以正确创建分组:
此时,返回的分组中的键将为您提供所需的列表。
It appears that you want to group on the date and id, so you want to pull those out into a structure that you can group on. It's when you create this key structure that you can format the date using the Date property to create the grouping correctly:
At that point, the keys in the grouping returned will give you the list you want.