Linq - where inside

I know that this will not work as it is written, but I try my best to find the right answer, and this non-functional code, hopefully, illustrates what I am trying to achieve:

var defaults = _cilQueryContext.DefaultCharges
                    .Where(dc => dc.ChargingSchedule_RowId == cs.RowId);

List<DevelopmentType> devTypes = 
        defaults.Select(dc => dc.DevelopmentType)
                .Include(d => d.DefaultCharges)
                .Include(d => d.OverrideCharges.Where(oc => oc.ChargingSchedule_RowId == cs.RowId))
                .Include(d => d.OverrideCharges.Select(o => o.Zone))
                .ToList();

Essentially, I assumed that this requires a connection, but, seeing that I am trying to select a parent containing two sibling types of children, I don’t see what will be in the join section "select new".

+4
source share
1 answer

As far as I know, it Includedoes not support this type of sub-queries. Your best option is to use a projection, for example.

List<DevelopmentType> devTypes = 
           defaults.Include(x => x.DefaultCharges)
                   .Include(x => x.OverrideCharges)
                   .Select(x => new {
                        DevType = x.DevelopmentType,
                        Zones = x.OverrideCharges.Where(oc => oc.ChargingSchedule_RowId == cs.RowId)
                                                 .Select(oc => oc.Zone).ToList()
                   })
                   .Select(x => x.DevType)
                   .ToList();
+2
source

All Articles