Question

I have this linq:

using (PlantContext context = ContextManager.GetPlantContext(plantId.PlantPath))
        {
            results = (from p in context.Plants
                       join subplant in context.Subplants on p.Id equals subplant.PlantId
                       join inverter in context.Inverters on subplant.Id equals inverter.SubplantId
                       join inverterActualData in context.InverterActualDatas on inverter.Id equals inverterActualData.InverterId into joinAll
                       from joinItem in joinAll.DefaultIfEmpty()
                       select new
                       {
                           ActualData = joinItem,
                           Inverter = inverter,
                           Subplant = subplant,
                           Plant = p
                       }).AsEnumerable().Select(r => r.Inverter).ToList();
        }

After that, I want to go through the results:

foreach (Inverter inverter in results)
{
     if (inverter.InverterActualData == null)
     {
     }
}

This throws the following exception on the "if (inverter.InverterActualData == null)" line:

An exception of type 'System.ObjectDisposedException' occurred in System.Data.Entity.dll but was not handled in user code

What I am trying to do is get the list of inverters, but at the same time load its Subplant and InverterActualData properties. (Also, for Subplant, load the Plant property).

The problem arises when there is no InverterActualData record for the Inverter. In this case, the exception is thrown.

Why I want to load all data before the context is closed? Because one trip to the database is much faster than to call the InverterActualData for each inverter separately.

As you can see, I am trying to check whether the InverterActualData property is null, but apparently is not loaded. The exception is thrown regardless.

I also tried adding:

join inverter in context.Inverters.Include(InverterExtension.Relations.InverterActualData) on subplant.Id equals inverter.SubplantId

But this doesn't work either.

It seems weird to me, since in profiler, the query is translated like this:

SELECT 1                                    AS [C1],
   [Extent4].[InverterId]               AS [InverterId],
   [Extent4].[Period]                   AS [Period],
   [Extent4].[OperatingHours]           AS [OperatingHours],
   [Extent3].[Id]                       AS [Id],
   [Extent3].[InverterTypeId]           AS [InverterTypeId],
   [Extent3].[SubplantId]               AS [SubplantId],
   [Extent3].[Name]                     AS [Name],
   [Extent2].[Id]                       AS [Id1],
   [Extent2].[PlantId]                  AS [PlantId],
   [Extent2].[Name]                     AS [Name1],
   [Extent1].[Id]                       AS [Id2],
   [Extent1].[Name]                     AS [Name2],
FROM   [Plant] AS [Extent1]
   INNER JOIN [Subplants] AS [Extent2] ON [Extent1].[Id] = [Extent2].[PlantId]
   INNER JOIN [Inverters] AS [Extent3] ON [Extent2].[Id] = [Extent3].[SubplantId]
   LEFT OUTER JOIN [InverterActualData] AS [Extent4] ON [Extent3].[Id] = [Extent4].[InverterId]

Which is exactly what I want. Also, running this query on the DB, gets me the Inverter, Subplant and Plant data and default values for InverterActualData.

What could be the problem?

Was it helpful?

Solution

Did it with:

using (PlantContext context = ContextManager.GetPlantContext(plantId.PlantPath))
        {
            context.ContextOptions.LazyLoadingEnabled = false;
            results = (from p in context.Plants
                       join subplant in context.Subplants on p.Id equals subplant.PlantId
                       join inverter in context.Inverters on subplant.Id equals inverter.SubplantId
                       join inverterActualData in context.InverterActualDatas on inverter.Id equals inverterActualData.InverterId into joinAll
                       from joinItem in joinAll.DefaultIfEmpty()
                       select new
                       {
                           ActualData = joinItem,
                           Inverter = inverter,
                           Subplant = subplant,
                           Plant = p
                       }).ToList().Select(r => r.Inverter).ToList();
        }

Here, the "context.ContextOptions.LazyLoadingEnabled = false;" and the first "ToList()" are essential

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top