使用子查询将 SQL 查询转换为 LINQ

问题描述 投票:0回答:1

我不懂 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
linq
1个回答
0
投票
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
})
© www.soinside.com 2019 - 2024. All rights reserved.