[英]Calculating DateTime with LINQ to SQL
I have two tables TimeSheet and TimeRecord. 我有两个表TimeSheet和TimeRecord。 TimeRecord has Foreign Key TimeSheetId of TimeSheet.
TimeRecord具有TimeSheet的外键TimeSheetId。 The following time-logs are from TimeRecord,
以下时间日志来自TimeRecord,
TimeSheet sample data: TimeSheet示例数据:
TimeSheetId, StudentId
187 , 10
196 , 11
195 , 12
TimeRecord sample data: TimeRecord样本数据:
TimeRecordId TimeSheetId TimeIn TimeOut
1 187 8/17/2010 1:06:55 PM 8/17/2010 1:53:49 PM
2 196 8/17/2010 1:31:28 PM 8/17/2010 4:59:58 PM
3 187 8/17/2010 1:51:40 PM 8/17/2010 4:59:02 PM
4 187 8/17/2010 2:13:35 PM 8/17/2010 5:00:08 PM
5 196 8/17/2010 2:19:44 PM 8/17/2010 5:00:14 PM
6 196 8/17/2010 2:23:02 PM 8/17/2010 4:46:00 PM
7 195 8/17/2010 3:04:15 PM 8/17/2010 4:58:34 PM
I'd like to get total time spent of each student. 我想得到每个学生的总时间。 So, the result will be like something's like the following:
因此,结果将类似于以下内容:
10 has 10hr 30mn 5sec
11 has 8hr 45mm 23sec
12 has 2hr 33mn 25sec
Thanks a lot. 非常感谢。
You can simply subtract one date from the other. 您只需从另一个中减去一个日期即可。 This will give you a TimeSpan object.
这将为您提供TimeSpan对象。 You can then use the ToString method of the TimeSpan object to display the time spent however you wish using standard TimeSpan format strings .
然后,您可以使用TimeSpan对象的ToString方法显示您希望使用标准TimeSpan格式字符串所花费的时间。 Your might look something like this:
你可能看起来像这样:
TimeSpan timeSpent = studentTimeRecord.TimeOut - studentTimeRecortd.TimeIn;
string displayTime = timeSpent.ToString("[hh]hr [mm]mn [ss]sec");
Update: I just realized that I did not address the issue of grouping the students and summing over the grouping. 更新:我刚刚意识到我没有解决将学生分组和总结分组的问题。 I'll defer to Jon's answer for that.
我会按照Jon的回答。
Well, I don't know how well it will work, but I'd at least try : 好吧,我不知道它会有多好用,但我至少会尝试 :
var query = from record in db.TimeRecords
group record by record.TimeSheetId into g
select new { id = g.Key,
TotalTime = g.Sum(x => x.TimeOut - x.TimeIn) } into t
join student in db.TimeSheets
on t.id equals student.TimeSheetId
select new { student.StudentId, t.TotalTime };
It really depends on whether LINQ to SQL is happy to subtract one DateTime from another and then sum the results of doing that across multiple records. 这实际上取决于LINQ to SQL是否乐于从另一个中减去一个DateTime,然后将这样做的结果与多个记录相加。 (If it works,
TotalTime
should end up as a TimeSpan
.) (如果有效,
TotalTime
最终应该作为TimeSpan
。)
I think this what you are trying to archive: 我想这就是你想要存档的内容:
Dim query = From ts In db.TimeSheet _
Join tr In db.TimeRecord On tr.TimeSheetId Equals ts.TimeSheetId _
Group By ts.StudentId, tr.TimeSheetId Into TotalTime = Sum(DateDiffSecond(tr.TimeIn, tr.TimeOut)) _
Select StudentId, TimeSheetId, TotalTime
Dim timespan As TimeSpan
Dim formattedTimeSpan As String
For Each q In query
timespan = timespan.FromSeconds(q.TotalTime)
formattedTimeSpan = String.Format("{0} hr {1} mm {2} sec", Math.Truncate(timespan.TotalHours), timespan.Minutes)
Response.Write("Student " & " " & q.StudentId & " has " & q.TimeSheetId & " : " & formattedTimeSpan & "<br/>")
Next
You need to import SqlMethods: 您需要导入SqlMethods:
Imports System.Data.Linq.SqlClient.SqlMethods
Also Check out: 还可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.