实体框架LEFT JOIN与CASE和COALESCE

时间:2016-12-24 13:40:48

标签: entity-framework left-join coalesce

我的遗留代码中有以下条款,我们需要转到新的EF解决方案。它用于进行货币转换。

SELECT TOP 25 job_id, j.salary_format_id, j.currency_id, j.offer_date, j.accepted_date, j.reported_date, j.start_date,

CASE     
    WHEN salary_format_id = 1 AND salary > 0 THEN salary * 1
    WHEN salary_format_id = 2 AND salary > 0 THEN salary * 4
    WHEN salary_format_id = 3 AND salary > 0 THEN salary * 12
    WHEN salary_format_id = 4 AND salary > 0 THEN salary * 52
    WHEN salary_format_id = 5 AND salary > 0 THEN salary * 5 * 52
    WHEN salary_format_id = 6 AND salary > 0 THEN salary * 40 * 52    
END * (c2.rate / c1.rate) AS salary

FROM job j
LEFT JOIN exchange_rate c1 ON CAST(
(
CASE 
    WHEN COALESCE(j.offer_date, j.accepted_date, j.reported_date, j.start_date) < '1999-01-01' THEN '1999-01-01'
    ELSE COALESCE(j.offer_date, j.accepted_date, j.reported_date, j.start_date)
END
) AS Date) = c1.date AND c1.currency_id = j.currency_id
LEFT JOIN exchange_rate c2 ON CAST(
(
    CASE 
      WHEN COALESCE(j.offer_date, j.accepted_date, j.reported_date, j.start_date) < '1999-01-01' THEN '1999-01-01'
      ELSE COALESCE(j.offer_date, j.accepted_date, j.reported_date, j.start_date)
    END
) AS Date) = c2.date AND c2.currency_id = @currency_id

我的意图是根据日期转换货币(我们将费率存储在&#39; exchnage_rate&#39;表中。它使用COALESCE确定要使用哪个日期列的优先顺序,以便获得正确的费率

我有以下一行(简化为了简化):

var baseQuery = this.dbContext.Jobs;
...

如何在此处将以下SQL转换为实体框架代码?

0 个答案:

没有答案