如何将以下 SQL 查询转换为 LINQ C#?我需要将查询拆分为多个查询吗?哪种方法更好?

问题描述 投票:0回答:1
SELECT DISTINCT 
    NULL AS encounterFlagId, AccountFlagID,
    CASE 
        WHEN ((SELECT COUNT(*) 
               FROM [APP].[AFPTable] afp 
               LEFT JOIN APP.PTable p ON afp.ProjectID= p.ProjectID AND p.Active = 1 
               WHERE afp.AccountFlagID = af.AccountFlagId 
                 AND afp.Active = 1 
                 AND p.ThroughDate IS NULL 
                 AND ProjectTypeID IN (SELECT ProjectTypeID  
                                       FROM APP.PTableType 
                                       WHERE AccountWorkable = 1)) >= 
              (SELECT COUNT(*) 
               FROM APP.PTable p 
               WHERE p.Active = 1 
                 AND p.ThroughDate IS NULL 
                 AND ProjectTypeID IN (SELECT ProjectTypeID 
                                       FROM APP.PTableType 
                                       WHERE AccountWorkable = 1))) 
            THEN 'ALL' 
            ELSE STUFF((SELECT ',' + CAST(APP.ufn_GetAbbre(p.Description, 1) AS varchar(300)) 
                        FROM [APP].[AFPTable] afp 
                        LEFT JOIN APP.PTable p ON afp.ProjectID = p.ProjectID AND p.Active = 1
                        WHERE afp.AccountFlagID = af.AccountFlagId 
                          AND afp.Active = 1 
                          AND p.ThroughDate IS NULL 
                          AND ProjectTypeID IN (SELECT ProjectTypeID  
                                                FROM APP.PTableType 
                                                WHERE AccountWorkable = 1) 
                        FOR XML PATH('')), 1, 1, '') END AS Projects
FROM [APP].[AFPTable] af
c# sql entity-framework linq query-optimization
1个回答
0
投票

将复杂的 SQL 查询转换为 LINQ 可能具有挑战性,但这绝对是可能的。以下是将 SQL 查询转换为 C# 中的 LINQ 的分步方法。我还将在可下载的 Markdown 文件中提供最终的 LINQ 查询。

逐步转换

  • 了解 SQL 查询: 将 SQL 查询分解为更小的部分以了解其结构和逻辑。

  • 创建数据上下文:确保您有一个映射到数据库表的数据上下文类。

  • 转换子查询:将每个子查询转换为 LINQ。 组合查询:将子查询组合到主查询中。

SQL 到 LINQ 转换

以下是如何将 SQL 查询转换为 LINQ 的简化版本:

var query = from af in context.AFPTable
            select new
            {
                encounterFlagId = (int?)null,
                af.AccountFlagID,
                Projects = (from afp in context.AFPTable
                            join p in context.PTable on afp.ProjectID equals p.ProjectID
                            where afp.AccountFlagID == af.AccountFlagID
                                  && afp.Active == 1
                                  && p.Active == 1
                                  && p.ThroughDate == null
                                  && context.PTableType.Any(pt => pt.ProjectTypeID == p.ProjectTypeID && pt.AccountWorkable == 1)
                            select afp).Count() >=
                          (from p in context.PTable
                           where p.Active == 1
                                 && p.ThroughDate == null
                                 && context.PTableType.Any(pt => pt.ProjectTypeID == p.ProjectTypeID && pt.AccountWorkable == 1)
                           select p).Count()
                          ? "ALL"
                          : string.Join(",", (from afp in context.AFPTable
                                              join p in context.PTable on afp.ProjectID equals p.ProjectID
                                              where afp.AccountFlagID == af.AccountFlagID
                                                    && afp.Active == 1
                                                    && p.Active == 1
                                                    && p.ThroughDate == null
                                                    && context.PTableType.Any(pt => pt.ProjectTypeID == p.ProjectTypeID && pt.AccountWorkable == 1)
                                              select context.ufn_GetAbbre(p.Description, 1)).ToList())
            };

文字是根据下面的链接和我的个人经验写的

开发者

geeksforgeeks.org

将sql查询转换为linq

在此输入链接描述

我的个人经历

© www.soinside.com 2019 - 2024. All rights reserved.