将SQL查询转换为带子查询的LINQ

c9qzyr3d  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(141)

我不知道linq,但我需要为此SQL查询创建一个linq查询

SELECT a.[ProjectId], 
            a.[CompanyId],          
            a.[Status], 
            ISNULL((SELECT TOP 1 1 
                      FROM [dbo].[Project] b 
                     WHERE b.[MasterProjectId] = a.[MasterProjectId] 
                       AND b.[Level] > 1), 0) [HasSubProjects] 
       FROM [dbo].[Project] a
ao218c7q

ao218c7q1#

var d = con.Projects;
var ret = d.Select(x => new { 
    x.ProjectId,
    x.CompanyId,
    x.Status,
    HasSubProjects = d.Any(y=> y.MasterProjectId == x.MasterProjectId && y.Level > 1) ? 1 : 0
})

相关问题