gpt4 book ai didi

entity-framework-core - 如何使用 Entity Framework Core 创建 T-SQL 交叉应用?

转载 作者:行者123 更新时间:2023-12-05 05:08:53 25 4
gpt4 key购买 nike

我想使用 Entity Framework Core 创建一个查询,该查询具有多个 INNER JOIN 和一个 CROSS APPLY。

我可以很好地创建 INNER JOIN,但似乎无法使 CROSS APPLY 工作。

  • 一些 Google 搜索是否没有找到真正有用的东西
  • 尝试使用 Entity Framework 的 linq 语句进行不同的尝试,但到目前为止无济于事。

这是我目前得到的 EF 查询:

comCommunicationContext
.Communication
.AsNoTracking()
.Where(c => c.Label.InternalName == labelName &&
c.Businesstransaction.DocumentType.DocumentTypeName == documentType &&
c.Businesstransaction.SourceSystem.SourceSystemName == sourceSystem)
.Join(comCommunicationContext.CommunicationOutputChannel, communicationEntity => communicationEntity.CommunicationId, communicationOutputChannelEntity => communicationOutputChannelEntity.CommunicationId, (communicationEntity, communicationOutputChannelEntity) => new {Communication = communicationEntity, CommunicationOutputChannel = communicationOutputChannelEntity})
.Where(y => y.CommunicationOutputChannel.OutputChannel == outputChannel)
.Join(comCommunicationContext.CommunicationStatusOutputChannel.
Where(x => x.Status=="to process"), communicationOutputChannelEntity => communicationOutputChannelEntity.CommunicationOutputChannel.CommunicationOutputChannelId, communicationStatusOutputChannelEntity => communicationStatusOutputChannelEntity.CommunicationOutputChannelId, (outer, inner) => new Communication() { CommunicationDataEnriched = outer.Communication.CommunicationDataEnriched })

生成以下 T-SQL 查询:

SELECT [c].[CommunicationDataEnriched]
FROM [Communications] AS [c]
INNER JOIN [Businesstransactions] AS [c.Businesstransaction] ON [c].[BusinessTransactionId] = [c.Businesstransaction].[BusinessTransactionId]
INNER JOIN [DocumentTypes] AS [c.Businesstransaction.DocumentType] ON [c.Businesstransaction].[DocumentTypeId] = [c.Businesstransaction.DocumentType].[DocumentTypeId]
INNER JOIN [SourceSystems] AS [c.Businesstransaction.SourceSystem] ON [c.Businesstransaction].[SourceSystemId] = [c.Businesstransaction.SourceSystem].[SourceSystemId]
INNER JOIN [Labels] AS [c.Label] ON [c].[LabelId] = [c.Label].[LabelId]
INNER JOIN [CommunicationOutputChannels] AS [CommunicationOutputChannels] ON [c].[CommunicationId] = [CommunicationOutputChannels].[CommunicationId]
INNER JOIN (
SELECT [x].*
FROM [CommunicationStatusOutputChannels] AS [x]
WHERE [x].[Status] = 'to process'
) AS [t] ON [CommunicationOutputChannels].[CommunicationOutputChannelId] = [t].[CommunicationOutputChannelId]
WHERE ((([c.Label].[InternalName] = 'labelname') AND ([c.Businesstransaction.DocumentType].[DocumentTypeName] = 'documenttype')) AND ([c.Businesstransaction.SourceSystem].[SourceSystemName] = 'sourcessysem')) AND ([CommunicationOutputChannels].[OutputChannel] = 'email')

我想发送给数据库的是这样的:

SELECT [c].[CommunicationDataEnriched]
FROM [Communications] AS [c]
INNER JOIN [Businesstransactions] AS [c.Businesstransaction] ON [c].[BusinessTransactionId] = [c.Businesstransaction].[BusinessTransactionId]
INNER JOIN [DocumentTypes] AS [c.Businesstransaction.DocumentType] ON [c.Businesstransaction].[DocumentTypeId] = [c.Businesstransaction.DocumentType].[DocumentTypeId]
INNER JOIN [SourceSystems] AS [c.Businesstransaction.SourceSystem] ON [c.Businesstransaction].[SourceSystemId] = [c.Businesstransaction.SourceSystem].[SourceSystemId]
INNER JOIN [Labels] AS [c.Label] ON [c].[LabelId] = [c.Label].[LabelId]
INNER JOIN [CommunicationOutputChannels] AS [CommunicationOutputChannels] ON [c].[CommunicationId] = [CommunicationOutputChannels].[CommunicationId]
CROSS APPLY (
SELECT TOP 1 [x].*
FROM [CommunicationStatusOutputChannels] AS [x]
WHERE [x].[Status] = 'to process'
AND [x].[CommunicationOutputChannelId] = [CommunicationOutputChannels].[CommunicationOutputChannelId]
ORDER BY [x].createdOn DESC
) [t]
WHERE ((([c.Label].[InternalName] = 'labelname') AND ([c.Businesstransaction.DocumentType].[DocumentTypeName] = 'documenttype')) AND ([c.Businesstransaction.SourceSystem].[SourceSystemName] = 'sourcessysem')) AND ([CommunicationOutputChannels].[OutputChannel] = 'email')

对象 CommunicationOutputChannel有一个导航属性 CommunicationStatusOutputChannels类型 ICollection<CommunicationStatusOutputChannel> (CommunicationOutputChannelCommunicationStatusOutputChannels 实体之间存在一对多关系)。

所以最后INNER JOIN表之间CommunicationOutputChannelsCommunicationStatusOutputChannels应该是 CROSS APPLY .

最佳答案

没关系。找到适合我的解决方案。

 var q = from c in comCommunicationContext.Communication.Where(c=>c.Label.InternalName==labelName && c.Businesstransaction.DocumentType.DocumentTypeName==documentType && c.Businesstransaction.SourceSystem.SourceSystemName==sourceSystem)
join coc in comCommunicationContext.CommunicationOutputChannel
on c.CommunicationId equals coc.CommunicationId
from csoc in comCommunicationContext.CommunicationStatusOutputChannel.Where(x=>x.CommunicationOutputChannelId==coc.CommunicationOutputChannelId).OrderByDescending(y=>y.CreatedOn).Take(1)
where csoc.Status == "to process"
select new Communication(){CommunicationDataEnriched = c.CommunicationDataEnriched}
;

关于entity-framework-core - 如何使用 Entity Framework Core 创建 T-SQL 交叉应用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58010606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com