如何计算LINQ中的总工作时间?

发布于 2024-10-20 06:01:33 字数 482 浏览 1 评论 0原文

我有一个表,其中保存每个用户的上班/下班记录:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

话少心凉 2024-10-27 06:01:33
  SELECT (SELECT SUM(DATEDIFF(SECOND,[ClockInOutTime], GETDATE()))
     FROM [swp].[dbo].[Table_1] t1
     WHERE [In/Out] = 'IN'
     AND t1.[User] = t.[User]) -
     Coalesce((SELECT SUM(DATEDIFF(SECOND,[ClockInOutTime], GETDATE()))
     FROM [swp].[dbo].[Table_1] t2
     WHERE [In/Out] = 'OUT'
     AND t2.[User] = t.[User]),0)
  FROM [swp].[dbo].[Table_1] t   
  GROUP BY [User]

SQL 方法来解决这个问题,不是最好的,但即使最后一个事件没有 OUT 时间戳,即最后一个会话仍未关闭时,它也可以工作。

  SELECT (SELECT SUM(DATEDIFF(SECOND,[ClockInOutTime], GETDATE()))
     FROM [swp].[dbo].[Table_1] t1
     WHERE [In/Out] = 'IN'
     AND t1.[User] = t.[User]) -
     Coalesce((SELECT SUM(DATEDIFF(SECOND,[ClockInOutTime], GETDATE()))
     FROM [swp].[dbo].[Table_1] t2
     WHERE [In/Out] = 'OUT'
     AND t2.[User] = t.[User]),0)
  FROM [swp].[dbo].[Table_1] t   
  GROUP BY [User]

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.

童话 2024-10-27 06:01:33

无论是在 Linq 中还是在 SQL 中,这都不是一件容易的事。没有简单的方法可以将每个 OUT 记录与 SQL 中相应的 IN 记录链接起来。

您有两种选择:

  1. 查询数据并在 for 循环内的代码中进行计算。
  2. 更改表架构,例如: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:

  1. Querying the data and calculating in code within a for loop.
  2. Changing the table schema like: 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 corresponding OUT record (or be last record)?

转身泪倾城 2024-10-27 06:01:33
TimeSpan output = new TimeSpan(0,0,0);
using (var enumerator = input.GetEnumerator())
{
    while (enumerator.MoveNext())
    {
      var begin = enumerator.Current.ClockInOutTime;
      if(!enumerator.MoveNext())
        break;

      var end = enumerator.Current.ClockInOutTime;
      output += (end - begin);
    }
}

是的,它不是 LINQ,但我想提供一个解决方案 - 其次,如果日期不交替(因此在 IN 后总是 OUT),它就会中断。

TimeSpan output = new TimeSpan(0,0,0);
using (var enumerator = input.GetEnumerator())
{
    while (enumerator.MoveNext())
    {
      var begin = enumerator.Current.ClockInOutTime;
      if(!enumerator.MoveNext())
        break;

      var end = enumerator.Current.ClockInOutTime;
      output += (end - begin);
    }
}

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.

微暖i 2024-10-27 06:01:33

没有只使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文