如何使用 Linq to Entity 重写 SQL 查询
我需要使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设 Context 已将所有相关表定义为实体并定义了正确的关系:
如果我对架构了解更多,我可能会做得更好一些。您最终会得到具有 ClientId 和 CompanyName 属性的匿名类型的集合;我个人倾向于避免匿名类型,除非相应的对象太大。
Assuming that
Context
has all the relevant tables defined already as entities and has proper relationships defined: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.