我有一个复杂的查询,必须转换为linq。我不知道如何使用select top 1语句转换cross apply并选择sum。这是查询:
SELECT C.LASTNAME, C.FIRSTNAME, U.IMP_REPORT_NAME, C.CLIENTID, U.IMP_USER_REPORT_ID, U.IMP_REPORT_DATE, U.EMPLOYEEID, TBL_RATINGS.TOTAL AS TOTAL_RATING
FROM CLIENTS AS C
CROSS APPLY ( SELECT TOP 1 IMP_USER_REPORT_ID, IMP_REPORT_NAME, IMP_REPORT_DATE, EMPLOYEEID
FROM IMPORT_USER_REPORTS
AND CLIENTID = C.CLIENTID
AND IMP_REPORT_DATE <= @fromDate
ORDER BY IMP_REPORT_DATE DESC, IMP_USER_REPORT_ID DESC
) AS U
CROSS APPLY ( SELECT SUM(ANSWER_RATING) AS TOTAL
FROM IMPORT_USER_ANSWERS
WHERE IMPORT_USER_ANSWERS.IMP_USER_REPORT_ID = U.IMP_USER_REPORT_ID
) AS TBL_RATINGS
WHERE C.ACTIVE = 1
ORDER BY LASTNAME, FIRSTNAME