1

我想通过使用 LINQ to SQL 从 ASP.NET 中的 TimeCheckIn 中减去 TimeCheckOut 来在数据 Gridview 中仅显示持续时间 Hour、Minutes 和 Second

这是后面的代码:

Dim db = new MyDataContext
Dim user = from u in db.Employees select IDNumber = u.IDNumber, _
           FirstName = u.firstName, LastName = u.lastName, TimeCheckIn = u.timeCheckIn, _
           TimeCheckOut = u.timeCheckOut, Duration = u.timeCheckIn
Gridview1.DataSource = user  
Gridview1.DataBind()

页面代码:

  <asp:GridView ID="GridView1" runat="server" Width="100%" 
    AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="IDNumber" HeaderText="ID Number" ReadOnly="True" />
        <asp:BoundField DataField="FirstName" HeaderText="First Name" ReadOnly="True"/>
        <asp:BoundField DataField="LastName" HeaderText="Last Name" ReadOnly="True"/>
        <asp:BoundField DataField="TimeCheckIn" HeaderText="Time Check In" ReadOnly="True"/>
        <asp:BoundField DataField="TimeCheckOut" HeaderText="Time Check Out" ReadOnly="True" />
        <asp:TemplateField HeaderText="Duration">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# FieldDisplayDuration(Eval("Duration")) %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

这是我的 FieldDisplayDuration 助手:

Protected Function FieldDisplayDuration(ByVal Duration As DateTime) As String
    Dim rtn As String = "DefaultValue"
    Dim dif As TimeSpan = DateTime.Now.Subtract(Duration)
        rtn = dif.Hours & " hours, " & dif.Minutes & " minutes, " & dif.Seconds & " seconds. "
    Return rtn
End Function

在 Helper 函数的第 3 行中,Dim dif as TimeSpan = DateTime.Now.Subtract(Duration) 仅给出从 TimeCheckIn 到 DateTime.Now 的小时、分钟和秒的持续时间。但是,我希望从 TimeCheckIn 到 TimeCheckOut 的持续时间仅以小时、分钟和秒为单位。我知道 FieldDisplayDuration 函数是完全错误的逻辑,但我只想让你明白我的意思,它也可能是那些想要计算员工从雇佣日期开始的持续时间的代码示例。最后,让我们通过从 gridview 问题中的 TimeCheckIn 中减去 TimeCheckOut 来回到 TimeSpan,我该怎么做?请给我一些线索..非常感谢你...

4

2 回答 2

0

可以只减去日期和时间来获得时间跨度,因此您应该能够使用

 Text='<%# FieldDisplayDuration(Eval("TimeCheckIn"), Eval("TimeCheckOut")) %>'

和类似这个功能的东西:

Protected Function FieldDisplayDuration(ByVal CheckIn As DateTime, ByVal CheckOut as DateTime) As String    
    Dim rtn As String = "DefaultValue"    
    Dim dif As TimeSpan = CheckOut - CheckIn        
    rtn = dif.Hours & " hours, " & dif.Minutes & " minutes, " & dif.Seconds & " seconds. "    
    Return rtnEnd 
Function

虽然我可能会在 rturn 行上使用字符串格式并执行此操作...

rtn = string.Format("{0} hours, {1} minutes, {2} seconds.", dif.Hours, dif.Minutes, difSeconds)

因为我认为它更容易阅读。

于 2009-05-18T03:27:11.750 回答
0

您可以将计算持续时间的逻辑放在 Select 子句中,如下所示:

Dim user = from u in db.Employees select IDNumber = u.IDNumber, _
       FirstName = u.firstName, LastName = u.lastName, TimeCheckIn = u.timeCheckIn, _
       TimeCheckOut = u.timeCheckOut, Duration = u.timeCheckOut.Subtract(u.timeCheckIn)

然后,您只需要格式化生成的时间跨度。

或者,您可以在选择中完成所有操作,甚至调用一个函数,所以有类似的东西

Duration = FormatDuration(u.timeCheckOut.Subtract(u.timeCheckIn)

或者

Duration = FormatDuration(u.timeCheckIn, u.timeCheckOut)\

如果你这样做了,并假设 FormatDuration 返回一个字符串,你将能够完全取消列模板

于 2009-05-18T03:29:48.357 回答