我试图弄清楚如何将此 SQL 选择转换为 LINQ,但我还没有弄清楚。
我正在获取每个 Id(PersonId)、每个测试、每个月、每年的最后一个测试记录。基本上,每年为每个人获取本月的最后一个分数。
CREATE TABLE #MyTable
(
Id INT,
Score INT,
TestName VARCHAR(50),
TestDate DATETIME
)
INSERT INTO #MyTable
VALUES
(1, 10, 'Math', '2011-12-16 00:00:00.000')
,(1, 25, 'Math', '2011-12-26 00:00:00.000')
,(1, 100, 'Math', '2011-12-06 00:00:00.000')
,(1, 10, 'Reading', '2011-12-16 00:00:00.000')
,(1, 25, 'Reading', '2011-12-26 00:00:00.000')
,(1, 100, 'Reading', '2011-12-06 00:00:00.000')
,(2, 10, 'Math', '2011-12-16 00:00:00.000')
,(2, 25, 'Math', '2011-12-26 00:00:00.000')
,(2, 100, 'Math', '2011-12-06 00:00:00.000')
,(2, 10, 'Reading', '2011-12-16 00:00:00.000')
,(2, 25, 'Reading', '2011-12-26 00:00:00.000')
,(2, 100, 'Reading', '2011-12-06 00:00:00.000')
,(1, 10, 'Math', '2011-12-16 00:00:00.000')
,(1, 25, 'Math', '2012-12-26 00:00:00.000')
,(1, 100, 'Math', '2012-12-06 00:00:00.000')
,(1, 10, 'Reading', '2012-12-16 00:00:00.000')
,(1, 25, 'Reading', '2012-12-26 00:00:00.000')
,(1, 100, 'Reading', '2012-12-06 00:00:00.000')
,(2, 10, 'Math', '2012-12-16 00:00:00.000')
,(2, 25, 'Math', '2012-12-26 00:00:00.000')
,(2, 100, 'Math', '2012-12-06 00:00:00.000')
,(2, 10, 'Reading', '2012-12-16 00:00:00.000')
,(2, 25, 'Reading', '2012-12-26 00:00:00.000')
,(2, 100, 'Reading', '2012-12-06 00:00:00.000')
SELECT DISTINCT
M.Id,M.Score,M.TestName, M.TestDate
FROM
#MyTable M
WHERE
M.TestDate IN (
SELECT MAX(m.TestDate)
FROM #MyTable m
GROUP BY MONTH(TestDate), YEAR(TestDate)
)
DROP TABLE
#MyTable
我有的:
from a in MyTable
where a.TestDate == from b in MyTable
group b.TestDate.Value.Month,
a.TestDate.Value.Year