2009-11-12 199 views
0

如何在linq中執行此查詢?所有表已經是對象列表。將此SQL轉換爲LINQ

該查詢給出了填充「Palavras」(詞)標準的名爲「Empresas」(公司)的實體的點。

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 

回答

2

我不認爲你會轉換,因爲它是從SQL到LINQ。你仍然可以試試這個工具,轉換SQL到LINQ語法:

http://www.sqltolinq.com/

的最好方法是理解和編寫你自己的LINQ語法。