简体   繁体   中英

EF 4 Filter sub collection

Im trying get all my maingroups with its related entities. And i would also like to filter the BuildingPartData collection without affecting any of the other entities. I have pretty much tried every thing i can think of but with no luck.

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

Here is the model

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

Cant insert images yet..

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

It only filtered my BuildingPartMainGroup entity. I would like to have all my BuildingPartMainGroup entities, with a condition / filter on BuildingPartData

Did you already think of something like:

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

You shouldn't use Any. Any evaluates to a boolean. So what you actually are doing is collecting BuildingPartMainGroup that have at a minimum 1 BuildingPartSubGroup that has a minimum of 1...that has a minimum of one BuildingPartData which has StatusPendingApprove == true.

Try considering chaining the Where<>() extension method all the way down the object graph.

Hope this helps...

EDIT:

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))))

I wonder if you need the include right here. If you're solely interested in the BuidlingPartMainGroups without the associated data it's not necessary.

A helpful tip could be to use an SQl Profiler to see the exact SQL query that EF generates. this way you gain some more understanding in what your LinqToEntities query generates for SQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM