如何将此SQL查询转换为LINQ查询

时间:2016-10-28 17:07:18

标签: sql entity-framework linq dbcontext

我正在尝试将此SQL查询转换为LINQ语法,但很难。 有人可以帮助我吗?

SELECT * 
FROM
    (SELECT
         DESCR,  
         SUM(CONSUMPTIONCOST) AS CUSUMPTIONCOST, 
         SUM(SCHEDULEDREPAIRCOST) AS SCHEDULEDREPAIRCOST,
         SUM(PALS) AS PALSUM,
         SUM(UNSCHEDULEDREPAIRCOST) AS UNSCHEDULEDREPAIRCOST,
         SUM(O_S_Price) AS O_S_PRICE_SUM,
         SUM(PALS) AS PALS,
         SUM(CGAI_TOTAL) AS CGAI_TOTAL,
         SUM(SE_TOTAL) AS SE_TOTAL,
         SUM(MISCADDER_TOTAL) AS MISCADDER_TOTAL,
         SUM(CODB_TOTAL) AS CODB_TOTAL,
         SUM(OS_PRICE_PLUSFACTORS) AS OS_PRICE_PLUSFACTORS,
         SUM(PALS + O_S_PRICE) AS COST,
         RANK() OVER(ORDER BY SUM(PALS + O_S_PRICE) DESC) AS COST_RANK
     FROM
         jamResult.Parts_Ace 
     GROUP BY 
         DESCR, RUN_NAME) AS X
WHERE 
    COST_RANK > 0 
    AND COST_RANK < 10 
ORDER BY 
    COST_RANK ASC

1 个答案:

答案 0 :(得分:0)

如果你正在寻找0到10之间的COST_RANK值(前9个结果按成本排名降级 - 排名为1到9的那些),你可以OrderByDescending进行LINQ,然后是Take(9)以缩小结果集:

var result = yourQuery.OrderByDescending(x => x.Cost).Take(9);