EF 4 Filter子集合

时间:2011-02-09 08:46:08

标签: .net entity-framework entity-framework-4

我试图让我所有的maingroups与其相关的实体。我还想过滤BuildingPartData集合而不影响任何其他实体。我几乎尝试了所有我能想到但却没有运气的东西。

 test.ContextOptions.LazyLoadingEnabled = false;

        var buildingPartMainGroups = (from buildingPartMainGroup in test.BuildingPartMainGroup
                                      from buildingPartSubGroup in buildingPartMainGroup.BuildingPartSubGroup
                                      from buildingPart in buildingPartSubGroup.BuildingPart
                                      from buildingPartData in buildingPart.BuildingPartData
                                      where buildingPartData.StatusPendingApprove == true
                                      //let buildingPartData = buildingPartMainGroup.BuildingPartSubGroup.Where(x => x.BuildingPart.Any(o => o.BuildingPartData.Any(y => y.StatusPendingApprove == true)))
                             select new
                             {
                                 BuildingPartMainGroups = buildingPartMainGroup,
                                 BuildingPartDatas = buildingPartData

                             }).ToList().Select(c => c.BuildingPartMainGroups);

        foreach (var bb in buildingPartMainGroups)
        {
            foreach(var tt in bb.BuildingPartSubGroup)
            {
                foreach (var oo in tt.BuildingPart)
                {
                    foreach (var ww in oo.BuildingPartData)
                    {
                        bool tes4t = ww.StatusPendingApprove;
                    }
                }
            }
        }

这是模型

http://mimo-design.com/model.png

无法插入图片..

var buildingPartMainGroups = test.BuildingPartMainGroup.Include("BuildingPartSubGroup.BuildingPart.BuildingPartData").
                                        Where(bpmg => bpmg.BuildingPartSubGroup.
                                            Any(o => o.BuildingPart.
                                                Any(x => x.BuildingPartData.
                                                    Any(u => u.StatusPendingApprove == true))));

它只过滤了我的BuildingPartMainGroup实体。我想拥有所有的BuildingPartMainGroup实体,并在BuildingPartData上使用条件/过滤器

2 个答案:

答案 0 :(得分:0)

你有没有想过这样的事情:

context.BuildingPartMainGroup
 .Inlcude("BuildingPartSubGroups.BuildingParts.BuildingPartData")
 .Where<BuildingPartMainGroup>(bpmg => bpmg.BuildingPartSubGroups.Any<BuildingPartSubGroup>(etc...

答案 1 :(得分:0)

你不应该使用任何。任何评估为布尔值。所以你实际在做的是收集至少有1个BuildingPartSubGroup的BuildingPartMainGroup,它至少有1个......至少有一个具有StatusPendingApprove == true的BuildingPartData。

尝试在对象图中一直考虑将Where&lt;&gt;()扩展方法链接起来。

希望这会有所帮助......

编辑:

context.BuildingPartMainGroups
    .Inlcude("BuildingPartSubGroups.BuildingParts.BuildingPartData")
    .Where<BuildingPartMainGroup>
    (bpmg => bpmg.BuildingPartSubGroups.Where<BuildingPartSubGroup>
    (bpsg => bpsg.BuildingParts.Where<BuildingPart>
    (bp => bp.BuildingPartData"s".Where<BuildingPartData>
    (bpd => bpd.StatusPendingApprove == true))))

我想知道你是否需要这里的包含。如果你只对没有相关数据的BuidlingPartMainGroup感兴趣,那就不必了。

一个有用的提示可能是使用SQl Profiler查看EF生成的确切SQL查询。通过这种方式,您可以更好地了解LinqToEntities查询为SQL生成的内容。