我有这个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属性)。
如果逆变器没有InverterActualData记录,则会出现问题。在这种情况下,抛出异常。
为什么我要在上下文关闭之前加载所有数据?因为一次到数据库的行程比分别为每个逆变器调用InverterActualData要快得多。
如您所见,我正在尝试检查InverterActualData属性是否为null,但显然未加载。无论如何抛出异常。
我也尝试添加:
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上运行此查询,可以获得InverterActualData的Inverter,Subplant和Plant数据以及默认值。
可能是什么问题?
答案 0 :(得分: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()”是必不可少的