La query generata da linq nonostante sia pazzesca e potrebbe essere ottimizzata mille volte ho provato ad eseguirla direttamente da sql menagment studio e di fatto viene eseguita perfettamente.
Ho utilizzato anche le proprietà di navigazione semplificate, ma di fatto la query generata è la stessa.
{"An error occurred while reading from the store provider's data reader. See the inner exception for details."}
InnerException:{"Timeout. Il tempo disponibile è scaduto prima del completamento dell'operazione o il server non risponde."}
Tempo di attesa scaduto.
La cosa assai anomala è che avvolte funziona altre volte no..
In particolare quando va in errore comunque il foreach passa un 600 record.
USE DB
SELECT
[Project10].[IDSottoconto] AS [IDSottoconto],
[Project10].[C1] AS [C1],
[Project10].[Sottoconto] AS [Sottoconto],
[Project10].[C2] AS [C2],
[Project10].[Descrizione] AS [Descrizione],
[Project10].[Gruppo] AS [Gruppo],
[Project10].[Descrizione1] AS [Descrizione1],
[Project10].[Conto] AS [Conto],
[Project10].[Descrizione2] AS [Descrizione2],
[Project10].[TipoConto] AS [TipoConto],
[Project10].[TipoClifor] AS [TipoClifor],
[Project10].[IDClifor] AS [IDClifor],
[Project10].[C3] AS [C3],
[Project10].[C4] AS [C4],
[Project10].[C5] AS [C5],
[Project10].[C6] AS [C6]
FROM ( SELECT
[Project9].[IDSottoconto] AS [IDSottoconto],
[Project9].[TipoClifor] AS [TipoClifor],
[Project9].[IDClifor] AS [IDClifor],
[Project9].[Gruppo] AS [Gruppo],
[Project9].[Conto] AS [Conto],
[Project9].[Sottoconto] AS [Sottoconto],
[Project9].[Descrizione] AS [Descrizione],
[Project9].[Descrizione1] AS [Descrizione1],
[Project9].[Descrizione2] AS [Descrizione2],
[Project9].[TipoConto] AS [TipoConto],
[Project9].[C1] AS [C1],
[Project9].[C2] AS [C2],
[Project9].[C3] AS [C3],
[Project9].[C4] AS [C4],
[Project9].[C5] AS [C5],
(SELECT
SUM([Extent17].[ImportoAvere]) AS [A1]
FROM [dbo].[PrimeNoteConti] AS [Extent17]
INNER JOIN [dbo].[PrimeNote] AS [Extent18] ON [Extent17].[IDPrimanota] = [Extent18].[IDPrimanota]
INNER JOIN [dbo].[TabellaContabilitaCausali] AS [Extent19] ON [Extent18].[IDCausale] = [Extent19].[IDCausale]
WHERE ([Extent18].[DataRegistrazione] >= 01/01/2016) AND (0 = [Extent19].[SadoFinale]) AND ([Project9].[IDSottoconto] = [Extent17].[IDSottoconto]) AND (([Project9].[TipoClifor] = [Extent17].[TipoClifor]) OR (([Project9].[TipoClifor] IS NULL) AND ([Extent17].[TipoClifor] IS NULL))) AND (([Project9].[IDClifor] = [Extent17].[IDClifor]) OR (([Project9].[IDClifor] IS NULL) AND ([Extent17].[IDClifor] IS NULL))) AND (0 = [Extent19].[SaldoIniziale])) AS [C6]
FROM ( SELECT
[Project8].[IDSottoconto] AS [IDSottoconto],
[Project8].[TipoClifor] AS [TipoClifor],
[Project8].[IDClifor] AS [IDClifor],
[Project8].[Gruppo] AS [Gruppo],
[Project8].[Conto] AS [Conto],
[Project8].[Sottoconto] AS [Sottoconto],
[Project8].[Descrizione] AS [Descrizione],
[Project8].[Descrizione1] AS [Descrizione1],
[Project8].[Descrizione2] AS [Descrizione2],
[Project8].[TipoConto] AS [TipoConto],
[Project8].[C1] AS [C1],
[Project8].[C2] AS [C2],
[Project8].[C3] AS [C3],
[Project8].[C4] AS [C4],
(SELECT
SUM([Extent14].[ImportoDare]) AS [A1]
FROM [dbo].[PrimeNoteConti] AS [Extent14]
INNER JOIN [dbo].[PrimeNote] AS [Extent15] ON [Extent14].[IDPrimanota] = [Extent15].[IDPrimanota]
INNER JOIN [dbo].[TabellaContabilitaCausali] AS [Extent16] ON [Extent15].[IDCausale] = [Extent16].[IDCausale]
WHERE ([Extent15].[DataRegistrazione] >= 01/01/2016) AND (0 = [Extent16].[SadoFinale]) AND ([Project8].[IDSottoconto] = [Extent14].[IDSottoconto]) AND (([Project8].[TipoClifor] = [Extent14].[TipoClifor]) OR (([Project8].[TipoClifor] IS NULL) AND ([Extent14].[TipoClifor] IS NULL))) AND (([Project8].[IDClifor] = [Extent14].[IDClifor]) OR (([Project8].[IDClifor] IS NULL) AND ([Extent14].[IDClifor] IS NULL))) AND (0 = [Extent16].[SaldoIniziale])) AS [C5]
FROM ( SELECT
[Project7].[IDSottoconto] AS [IDSottoconto],
[Project7].[TipoClifor] AS [TipoClifor],
[Project7].[IDClifor] AS [IDClifor],
[Project7].[Gruppo] AS [Gruppo],
[Project7].[Conto] AS [Conto],
[Project7].[Sottoconto] AS [Sottoconto],
[Project7].[Descrizione] AS [Descrizione],
[Project7].[Descrizione1] AS [Descrizione1],
[Project7].[Descrizione2] AS [Descrizione2],
[Project7].[TipoConto] AS [TipoConto],
[Project7].[C1] AS [C1],
[Project7].[C2] AS [C2],
[Project7].[C3] AS [C3],
(SELECT
SUM([Extent11].[ImportoAvere]) AS [A1]
FROM [dbo].[PrimeNoteConti] AS [Extent11]
INNER JOIN [dbo].[PrimeNote] AS [Extent12] ON [Extent11].[IDPrimanota] = [Extent12].[IDPrimanota]
INNER JOIN [dbo].[TabellaContabilitaCausali] AS [Extent13] ON [Extent12].[IDCausale] = [Extent13].[IDCausale]
WHERE ([Extent12].[DataRegistrazione] >= 1/1/2016 ) AND (0 = [Extent13].[SadoFinale]) AND ([Project7].[IDSottoconto] = [Extent11].[IDSottoconto]) AND (([Project7].[TipoClifor] = [Extent11].[TipoClifor]) OR (([Project7].[TipoClifor] IS NULL) AND ([Extent11].[TipoClifor] IS NULL))) AND (([Project7].[IDClifor] = [Extent11].[IDClifor]) OR (([Project7].[IDClifor] IS NULL) AND ([Extent11].[IDClifor] IS NULL))) AND ([Extent13].[SaldoIniziale] = 1)) AS [C4]
FROM ( SELECT
[Project6].[IDSottoconto] AS [IDSottoconto],
[Project6].[TipoClifor] AS [TipoClifor],
[Project6].[IDClifor] AS [IDClifor],
[Project6].[Gruppo] AS [Gruppo],
[Project6].[Conto] AS [Conto],
[Project6].[Sottoconto] AS [Sottoconto],
[Project6].[Descrizione] AS [Descrizione],
[Project6].[Descrizione1] AS [Descrizione1],
[Project6].[Descrizione2] AS [Descrizione2],
[Project6].[TipoConto] AS [TipoConto],
[Project6].[C1] AS [C1],
[Project6].[C2] AS [C2],
(SELECT
SUM([Extent8].[ImportoDare]) AS [A1]
FROM [dbo].[PrimeNoteConti] AS [Extent8]
INNER JOIN [dbo].[PrimeNote] AS [Extent9] ON [Extent8].[IDPrimanota] = [Extent9].[IDPrimanota]
INNER JOIN [dbo].[TabellaContabilitaCausali] AS [Extent10] ON [Extent9].[IDCausale] = [Extent10].[IDCausale]
WHERE ([Extent9].[DataRegistrazione] >= 01/01/2016) AND (0 = [Extent10].[SadoFinale]) AND ([Project6].[IDSottoconto] = [Extent8].[IDSottoconto]) AND (([Project6].[TipoClifor] = [Extent8].[TipoClifor]) OR (([Project6].[TipoClifor] IS NULL) AND ([Extent8].[TipoClifor] IS NULL))) AND (([Project6].[IDClifor] = [Extent8].[IDClifor]) OR (([Project6].[IDClifor] IS NULL) AND ([Extent8].[IDClifor] IS NULL))) AND ([Extent10].[SaldoIniziale] = 1)) AS [C3]
FROM ( SELECT
[Project2].[IDSottoconto] AS [IDSottoconto],
[Project2].[TipoClifor] AS [TipoClifor],
[Project2].[IDClifor] AS [IDClifor],
[Limit4].[Gruppo] AS [Gruppo],
[Limit4].[Conto] AS [Conto],
[Limit4].[Sottoconto] AS [Sottoconto],
[Limit4].[Descrizione] AS [Descrizione],
[Limit4].[Descrizione1] AS [Descrizione1],
[Limit4].[Descrizione2] AS [Descrizione2],
[Limit4].[TipoConto] AS [TipoConto],
[Limit4].[C1] AS [C1],
[Limit4].[C2] AS [C2]
FROM (SELECT
[Distinct1].[IDSottoconto] AS [IDSottoconto],
[Distinct1].[TipoClifor] AS [TipoClifor],
[Distinct1].[IDClifor] AS [IDClifor]
FROM ( SELECT DISTINCT
[Extent1].[IDSottoconto] AS [IDSottoconto],
[Extent1].[TipoClifor] AS [TipoClifor],
[Extent1].[IDClifor] AS [IDClifor]
FROM [dbo].[PrimeNoteConti] AS [Extent1]
INNER JOIN [dbo].[PrimeNote] AS [Extent2] ON [Extent1].[IDPrimanota] = [Extent2].[IDPrimanota]
INNER JOIN [dbo].[TabellaContabilitaCausali] AS [Extent3] ON [Extent2].[IDCausale] = [Extent3].[IDCausale]
WHERE ([Extent2].[DataRegistrazione] >= 01/01/2016) AND (0 = [Extent3].[SadoFinale])
) AS [Distinct1] ) AS [Project2]
OUTER APPLY (SELECT TOP (1)
[Project4].[Gruppo] AS [Gruppo],
[Project4].[Conto] AS [Conto],
[Project4].[Sottoconto] AS [Sottoconto],
[Project4].[Descrizione] AS [Descrizione],
[Project4].[Descrizione1] AS [Descrizione1],
[Project4].[Descrizione2] AS [Descrizione2],
[Limit3].[TipoConto] AS [TipoConto],
1 AS [C1],
[Project4].[Gruppo] + [Project4].[Conto] + [Project4].[Sottoconto] AS [C2]
FROM (SELECT
[Project3].[Gruppo] AS [Gruppo],
[Project3].[Conto] AS [Conto],
[Project3].[Sottoconto] AS [Sottoconto],
[Project3].[Descrizione] AS [Descrizione],
[Project3].[Descrizione1] AS [Descrizione1],
[Limit2].[Descrizione] AS [Descrizione2]
FROM (SELECT
[Filter2].[Gruppo] AS [Gruppo],
[Filter2].[Conto] AS [Conto],
[Filter2].[Sottoconto] AS [Sottoconto],
[Filter2].[Descrizione] AS [Descrizione],
[Limit1].[Descrizione] AS [Descrizione1]
FROM (SELECT [Extent4].[Gruppo] AS [Gruppo], [Extent4].[Conto] AS [Conto], [Extent4].[Sottoconto] AS [Sottoconto], [Extent4].[Descrizione] AS [Descrizione]
FROM [dbo].[PiacoSottoconti] AS [Extent4]
WHERE [Extent4].[IDSottoconto] = [Project2].[IDSottoconto] ) AS [Filter2]
OUTER APPLY (SELECT TOP (1) [Extent5].[Descrizione] AS [Descrizione]
FROM [dbo].[PiacoGruppi] AS [Extent5]
WHERE [Extent5].[Gruppo] = [Filter2].[Gruppo] ) AS [Limit1] ) AS [Project3]
OUTER APPLY (SELECT TOP (1) [Extent6].[Descrizione] AS [Descrizione]
FROM [dbo].[PiacoConti] AS [Extent6]
WHERE ([Extent6].[Gruppo] = [Project3].[Gruppo]) AND ([Extent6].[Conto] = [Project3].[Conto]) ) AS [Limit2] ) AS [Project4]
OUTER APPLY (SELECT TOP (1) [Extent7].[TipoConto] AS [TipoConto]
FROM [dbo].[PiacoConti] AS [Extent7]
WHERE ([Extent7].[Gruppo] = [Project4].[Gruppo]) AND ([Extent7].[Conto] = [Project4].[Conto]) ) AS [Limit3] ) AS [Limit4]
) AS [Project6]
) AS [Project7]
) AS [Project8]
) AS [Project9]
) AS [Project10]