我写了一个SQL脚本,但我无法理解如何将其翻译成EF Linq查询。
CREATE TABLE #MyTbl
(
ID INT IDENTITY,
UserProfileID INT,
IndustryName NVARCHAR(255),
IsPresent BIT,
EndDate DATETIME NULL
);
INSERT #MyTbl VALUES
(123, 'Banking', 0, '2016-01-01'),
(123, 'Education', 0, '2015-01-01'),
(123, 'Internet', 0, '2014-01-01'),
(123, 'Banking', 0, '2013-01-01'),
(456, 'Banking', 1, NULL),
(456, 'Banking', 1, NULL);
--------------------------------
How to write this in linq?
--------------------------------
SELECT t.UserProfileId,
t.IndustryName,
t.MaxEndDate
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY UserProfileId ORDER BY EndDate DESC) row_num,
UserProfileId,
IndustryName,
MAX(ISNULL(EndDate, DATEADD(year, 100, GETDATE()))) MaxEndDate
FROM #MyTbl
WHERE IndustryName IS NOT NULL
GROUP BY UserProfileId, IndustryName, EndDate
)t
WHERE t.row_num = 1;
DROP TABLE #MyTbl;
临时表就是例如。
也许有可能更简单地编写tSQL查询,然后将它转换为linq会更容易。
你能帮我吗?
提前致谢。