Я хочу создать запрос с помощью Entity Framework Core, который имеет несколько INNER JOIN и одно CROSS APPLY.
Я могу прекрасно создать ВНУТРЕННИЕ СОЕДИНЕНИЯ, но, похоже, не работает CROSS APPLY.
- Неужели в поиске по шине Google ничего толкового не получилось
- Пробовал разные вещи с помощью операторов linq Entity Framework, но пока безрезультатно.
Это 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>
(существует отношение один-ко-многим между CommunicationOutputChannel
и CommunicationStatusOutputChannels
лица).
Таким образом, последние INNER JOIN
между таблицами CommunicationOutputChannels
и CommunicationStatusOutputChannels
должны быть CROSS APPLY
.