如何将T-SQL转换为LINQ

时间:2013-04-13 09:25:57

标签: sql sql-server linq tsql

我在T-SQL查询中有以下代码,我需要将其转换(重写)为LINQ。有人能帮助我吗?感谢

SELECT (select max(X.PocetDniPoPlatnosti) 
from
(

select 

(select top 1 datediff(day,datumplatnosti,getdate()) from planrealizace p 
where p.cinnostsopidsop = cinnostsop.idsop and datumplatnosti <= getdate() and p.provest = 1 and p.datumprovedeni is null
order by p.datumplatnosti desc) as PocetDniPoPlatnosti

from cinnostsop
where cinnostSOP.LegislativneVyznamna = 1 and (CinnostSOP.ObjektId = 131476)) X) as PoPlatnosti

1 个答案:

答案 0 :(得分:0)

这应该可行,但我无法尝试,因为我家里没有任何编程工具,所以如果这不起作用,请随时告诉我,我们可以一起改进它

var max = (from cp in cinnostsop.Where(c => c.LegislativneVyznamna = 1 && c.ObjektId = 131476) 
           join p in (
               planrealizace.
               Where(pz => pz.datumplatnosti <= DateTime.Now && 
                           pz.provest = 1 and pz.datumprovedeni is null).
               GroupBy(pz => pz.cinnostsopidsop, pz => pz).
               Select(g => 
                   new { 
                           id = g.Key, 
                           firstdate = (g.OrderByDescending(
                                              pz => pz.datumplatnosti).
                                          First().datumplatnosti - DateTime.Now
                                        ).Totaldays
                        })
               ) on cp.idsop equals p.id
           select p.firstdate).Max(d => d);