简体   繁体   中英

C# LINQ Filter deep nested list

I've a structure based of list containing other list. I need to filter the list based on the value of a property based in the deepest part of the list.

Right now I'm doing this:

queryable = queryable
    .Include(x => x.Carriers)
    .ThenInclude(c => c.CarrierActions)
    .ThenInclude(ca => ca.Action)
    .ThenInclude(ac => ac.ActionFacts);

queryable = queryable
    .Where(x => x.Carriers.Any(
        carriers => carriers.CarrierActions.Any(
            carrieractions =>
                carrieractions.Action.ActionTypeId ==
                ActionTypeEnum.DosemeterCalculateDeepDose)));

I join the needed tables, then I filter them based on the ActionTypeId based 3 levels below the top list.

First off all, is it possible to do this in 1 step ( include the filtering with the joins ), second of all, the second part is not working as my list gets empty, but I'm certain that actions with that type get values.

Using .NET Core 2.0.3 btw!

To answer your first part, you can do this

queryable = queryable
    .Include(x => x.Carriers)
    .ThenInclude(c => c.CarrierActions)
    .ThenInclude(ca => ca.Action)
    .ThenInclude(ac => ac.ActionFacts)
    .Where(x => x.Carriers.Any(
        carriers => carriers.CarrierActions.Any(
            carrieractions =>
                carrieractions.Action.ActionTypeId ==
                ActionTypeEnum.DosemeterCalculateDeepDose)))

To your second part, it should be working, id check your data, as this is pretty straight forward

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