将此SQL转换为LINQ

时间:2009-11-12 17:35:29

标签: sql linq

如何在linq中执行此查询?所有表都已经是对象列表。

此查询为名为“Empresas”(公司)的实体提供了填充“Palavras”(Words)标准的实体。

select x.empresaid, sum(x.pontos)

from (

        select a.empresaid, sum(1) as Pontos
        from empresa  a
        inner join Palavras b on a.nome like '%' + b.Palavra + '%'
        group by a.empresaid

        union all

        select a.empresaid, sum(case when c.estabelecimento is null then 0 else 1 end) as Pontos
        from empresa  a
        left join estabelecimentoempresa b on b.empresaid = a.empresaid
        left join estabelecimento c on c.estabelecimentoid = b.estabelecimentoid
        left join Palavras d on c.estabelecimento like '%' + d.Palavra + '%'
        group by a.empresaid

        union all

        select a.empresaid, sum(case when c.Cozinha is null then 0 else 1 end) as Pontos
        from empresa  a
        left join Cozinhaempresa b on b.empresaid = a.empresaid
        left join Cozinha c on c.Cozinhaid = b.Cozinhaid
        left join Palavras d on c.Cozinha like '%' + d.Palavra + '%'
        group by a.empresaid
    ) x

group by x.empresaid

order by sum(x.pontos) desc, x.empresaid

1 个答案:

答案 0 :(得分:2)

我认为你无法转换,因为它是从SQL到LINQ。您仍然可以尝试将SQL转换为LINQ语法的工具:

http://www.sqltolinq.com/

最好的方法是自己理解和编写LINQ语法。