我需要从中组织考勤表
+--------+-----------+-----------+
| userId | CheckTime | CheckType |
+--------+-----------+-----------+
| 1 | 09:10 | In |
+--------+-----------+-----------+
| 1 | 16:02 | In |
+--------+-----------+-----------+
| 1 | 19:50 | Out |
+--------+-----------+-----------+
对此
+--------+---------+----------+
| userId | CheckIn | CheckOut |
+--------+---------+----------+
| 1 | 09:10 | |
+ +---------+----------+
| | 16:02 | 19:50 |
+ +---------+----------+
| | | |
+--------+---------+----------+
在Razor引擎中,我尝试了以下操作
<table class="table table-bordered table-striped">
@foreach (var usergroup in Model.Usersquery.GroupBy(g => g.UserId))
{
foreach (var checkin in Model.Attendancequery.Where(a => a.UserId == usergroup.Key && a.CheckType == "In"))
{
var checkout = @Model.Attendancequery.Where(a => a.UserId == usergroup.Key && a.CheckType == "Out" && a.CheckTime > checkin.CheckTime).Select(d => (DateTime?)d.CheckTime).DefaultIfEmpty().FirstOrDefault();
var hours = checkout - checkin.CheckTime;
<tr>
<th>@usergroup.FirstOrDefault(f => f.UserId == usergroup.Key).Name</th>
<td>@checkin.CheckTime.ToShortTimeString()</td>
@if (checkout.HasValue)
{
var cc = (DateTime)checkout;
<td>@cc.ToShortTimeString()</td>
} else{<td>@checkout</td>}
<td>@hours</td>
</tr>
}
}
</table>
问题在于,它为每次签入都计数相同的签出。 我很乐意提供帮助...