我尝试了下面的linq到sql查询
DataTable StudentTable=StudentTimeReport.Tables[0];
DataTable RoundEndDateTable=RoundEndDates.Tables[0];
var studentData = from student in StudentTable.AsEnumerable()
join Rounds in RoundEndDateTable.AsEnumerable() on student.Field<string>("strActivityName") equals Rounds.Field<string>("strActivityName")
select student;这两种数据如下
RoundEndDateTable
ActionName End Date
SQLs 8/31/2014 0:00
Gas Lekage 9/18/2014 0:00
Test Activity 9/20/2014 0:00StudentTable
ActionName Player ID Completed Date
Gas Lekage 810045 9/16/2014 14:38
Test Activity 810015 9/16/2014 14:39我需要得到球员的日期差,如下所示
Player ID Action Name Date difference
810015 Gas leakage 2
810015 Test Activity 4我不知道如何使用Linq到SQL获得具有日期差异的上表。
发布于 2014-09-23 04:12:39
如果我正确地理解了您的问题,您需要ActionName映射的两个表,并且您希望为每个玩家找出CompletedDate和Date之间的天数差异。
SELECT PlayerID, ActionName, MAX(DATEDIFF(DAY, Table1.Date, Table2.CompletedDate)) AS DateDifference
FROM Table1
INNER JOIN Table2 ON Table1.ActionName = Table2.ActionName
GROUP BY PlayerID, ActionName为了确保每个球员都有一个结果,正如你会注意到的,我采取最大的差别。这当然是你的决定,无论你是否需要,但我认为你做到了,考虑到你的问题的标题。
发布于 2014-09-23 04:12:47
我们可以使用datediff函数
select B.ActionName,
B.PlayerId,
DATEDIFF(day, A.Date, B.completedDate) as difference
FROM B
INNER JOIN A
ON B.ActionName = A.ActionName发布于 2014-09-23 04:57:18
var studentData = from redt in RoundEndDateTable
join st in StudentTable on redt.ActionName equals st.ActionName
select new
{
st.PlayerId,
st.ActionName,
((DateTime)st.Completed - (DateTime)redt.Date).TotalDays
}.ToList();请检查一下这个。
https://stackoverflow.com/questions/25986688
复制相似问题