如何使用 Linq to Entity 重写 SQL 查询

发布于 2024-10-25 14:01:54 字数 867 浏览 2 评论 0原文

我需要使用 Linq to Entity 重写下面的查询。有人知道如何以最充分的方式做到这一点吗?

   SELECT DISTINCT
        C.ClientId,
        C.CompanyName
    FROM Application A WITH (NOLOCK)
    INNER JOIN
                (
                SELECT ApplicationId
                FROM CAContracts WITH (NOLOCK)
                WHERE ID = 1212 AND CAContractStatusId IN (2,3)
                UNION ALL
                SELECT  OBA.ApplicationId
                FROM OpportunityAssignment OA WITH (NOLOCK)
                INNER JOIN OpportunityByApp OBA WITH (NOLOCK) ON
                    OBA.OpportunityId = OA.OpportunityId
                WHERE OA.ID = 1212
                    AND OA.OpporStatusId IN (5,7)
                ) ACPA ON
        ACPA.ApplicationId = A.Applicationid
    INNER JOIN Client C WITH (NOLOCK) ON
        C.ClientId = A.ClientId
    ORDER BY C.CompanyName

I need to rewrite the query below using Linq to Entity. Does someone know how to do it the most sufficient way?

   SELECT DISTINCT
        C.ClientId,
        C.CompanyName
    FROM Application A WITH (NOLOCK)
    INNER JOIN
                (
                SELECT ApplicationId
                FROM CAContracts WITH (NOLOCK)
                WHERE ID = 1212 AND CAContractStatusId IN (2,3)
                UNION ALL
                SELECT  OBA.ApplicationId
                FROM OpportunityAssignment OA WITH (NOLOCK)
                INNER JOIN OpportunityByApp OBA WITH (NOLOCK) ON
                    OBA.OpportunityId = OA.OpportunityId
                WHERE OA.ID = 1212
                    AND OA.OpporStatusId IN (5,7)
                ) ACPA ON
        ACPA.ApplicationId = A.Applicationid
    INNER JOIN Client C WITH (NOLOCK) ON
        C.ClientId = A.ClientId
    ORDER BY C.CompanyName

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守望孤独 2024-11-01 14:01:54

假设 Context 已将所有相关表定义为实体并定义了正确的关系:

Context.Clients
       .Include("Applications")
       .Include("Applications.CAContracts")
       .Include("Applications.OpportunityAssignments")
       .Include("Applications.OpportunityAssignments.OpportunityByApps")
       .Where<Client>(c => (c.Applications
                             .Any<Application>(a => a.CAContracts
                                                     .Any<CAContract>(cac => cac.ID == 1212 && (cac.CAContractStatusId == 2 || cac.CAContractStatusId == 3)))
                        || (c.Applications
                             .Any<Application>(a => a.OpportunityAssignments
                                                     .Any<OpportunityAssignment>(oa => oa.ID == 1212 && (oa.OpporStatusId == 5 || oa.OpporStatusId == 7) && oa.OpportunityByApps.Any<OpportunityByApp>()))))
       .Select(c => new { ClientId = c.ClientId, CompanyName = c.CompanyName})
       .Distinct()
       .OrderBy(c => c.CompanyName);

如果我对架构了解更多,我可能会做得更好一些。您最终会得到具有 ClientId 和 CompanyName 属性的匿名类型的集合;我个人倾向于避免匿名类型,除非相应的对象太大。

Assuming that Context has all the relevant tables defined already as entities and has proper relationships defined:

Context.Clients
       .Include("Applications")
       .Include("Applications.CAContracts")
       .Include("Applications.OpportunityAssignments")
       .Include("Applications.OpportunityAssignments.OpportunityByApps")
       .Where<Client>(c => (c.Applications
                             .Any<Application>(a => a.CAContracts
                                                     .Any<CAContract>(cac => cac.ID == 1212 && (cac.CAContractStatusId == 2 || cac.CAContractStatusId == 3)))
                        || (c.Applications
                             .Any<Application>(a => a.OpportunityAssignments
                                                     .Any<OpportunityAssignment>(oa => oa.ID == 1212 && (oa.OpporStatusId == 5 || oa.OpporStatusId == 7) && oa.OpportunityByApps.Any<OpportunityByApp>()))))
       .Select(c => new { ClientId = c.ClientId, CompanyName = c.CompanyName})
       .Distinct()
       .OrderBy(c => c.CompanyName);

If I knew more about the schema, I might be able to do a little better. You end up with a collection of anonymous types with ClientId and CompanyName properties; I tend to avoid anonymous types personally unless the corresponding object would otherwise be excessively large.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文