如何使用select top to linq转换交叉应用

时间:2015-12-24 11:35:07

标签: sql linq

我有一个复杂的查询,必须转换为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 

1 个答案:

答案 0 :(得分:0)

CROSS APPLY类似于需要更复杂条件的JOIN,因此WHERE可以按建议here使用。