1

我有这个 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();
        }

之后,我想查看结果:

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

这会在“if (inverter.InverterActualData == null)”行引发以下异常:

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

我要做的是获取逆变器列表,但同时加载其 Subplant 和 InverterActualData 属性。(另外,对于 Subplant,加载 Plant 属性)。

当 Inverter 没有 InverterActualData 记录时,就会出现问题。在这种情况下,将引发异常。

为什么我想在上下文关闭之前加载所有数据?因为一次访问数据库比分别为每个逆变器调用 InverterActualData 快得多。

如您所见,我正在尝试检查 InverterActualData 属性是否为空,但显然未加载。无论如何都会抛出异常。

我还尝试添加:

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

但这也不起作用。

这对我来说似乎很奇怪,因为在分析器中,查询被翻译成这样:

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]

这正是我想要的。此外,在 DB 上运行此查询,可以获得 Inverter、Subplant 和 Plant 数据以及 InverterActualData 的默认值。

可能是什么问题呢?

4

1 回答 1

0

做到了:

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();
        }

在这里,“context.ContextOptions.LazyLoadingEnabled = false;” 并且第一个“ToList()”是必不可少的

于 2013-04-26T14:05:11.423 回答