如何计算LINQ中的总工作时间?
我有一个表,其中保存每个用户的上班/下班记录:
RecID User In/Out ClockInOutTime
8 1 IN 25/02/2011 09:36:44
9 1 OUT 25/02/2011 11:36:44
10 1 IN 25/02/2011 12:36:44
11 1 OUT 25/02/2011 17:36:44
12 1 IN 26/02/2011 00:00:00
13 1 OUT 26/02/2011 12:00:00
14 1 IN 26/02/2011 09:00:44
15 1 OUT 26/02/2011 12:36:44
有什么想法可以使用 LINQ 计算出每个月的总工作时间吗?
干杯
I have a table which holds clocking in/out records for every user :
RecID User In/Out ClockInOutTime
8 1 IN 25/02/2011 09:36:44
9 1 OUT 25/02/2011 11:36:44
10 1 IN 25/02/2011 12:36:44
11 1 OUT 25/02/2011 17:36:44
12 1 IN 26/02/2011 00:00:00
13 1 OUT 26/02/2011 12:00:00
14 1 IN 26/02/2011 09:00:44
15 1 OUT 26/02/2011 12:36:44
Any ideas how I can work out the total time worked for every month using LINQ?
cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SQL 方法来解决这个问题,不是最好的,但即使最后一个事件没有 OUT 时间戳,即最后一个会话仍未关闭时,它也可以工作。
SQL way to solve this, not the best, but works even when last event don't have OUT timestamp i.e when last session still hasn't been closed.
无论是在 Linq 中还是在 SQL 中,这都不是一件容易的事。没有简单的方法可以将每个 OUT 记录与 SQL 中相应的 IN 记录链接起来。
您有两种选择:
RecID、User、ClockInTime、ClockOutTime
选项 1 很容易实现,但我会认真考虑选项 2。您如何在业务规则中定义每个
IN< /code> 记录后面必须跟有相应的
OUT
记录(或者是最后一条记录)?This is non-trivial to do in either Linq or SQL. There is no easy way to link each OUT record with the corresponding IN record in SQL.
You have two options:
RecID, User, ClockInTime, ClockOutTime
Option 1 is easy to implement, but I would seriously consider option 2. How do you define in your business rules that each
IN
record must be followed by a correspondingOUT
record (or be last record)?是的,它不是 LINQ,但我想提供一个解决方案 - 其次,如果日期不交替(因此在 IN 后总是 OUT),它就会中断。
Yes, it isn't LINQ but I wanted to offer a solution - secondly, if the dates aren't alternating (so after an IN is always an OUT) it'll break.
没有只使用 linq 的解决方案。这是因为您还需要引入错误处理(如果用户忘记注销,通常会有一个最大时间限制,等等)。
我将按用户对数据进行分组,按日期时间对其进行排序,然后在 foreach 中运行它并在 foreach 中进行计算。
There is no solution that will only use linq. This is due to the fact that you need to introduce error handling as well (if a user forgets to sign out, usually there is a maximum time that will be applied then etc).
I would group the data by user, order it by date time and then run through it in a for each and do the calculation within the for each.