简体   繁体   中英

WHERE is not being included in the LINQ-query

Today I ran into a problem with Entity Framework. I'm not sure if this is a weird bug or that i'm doing something wrong. I've already looked all over the forum for any possible solutions, but none I found worked for me.

I have the following LINQ query:

return (from sp in context.ServiceProviders.DefaultIfEmpty()
    join pl in context.Platforms on sp.Id equals pl.ServiceProviderId into innerPl
    from pl in innerPl.DefaultIfEmpty()
    join pp in context.Participants on pl.Id equals pp.PlatformId into innerPp
    from pp in innerPp.DefaultIfEmpty()
    join ps in context.Paymentsettlements on pp.Id equals ps.ParticipantId into innerPs
    from ps in innerPs.Where(ps => ps.ConfirmedOn.HasValue && ps.ExportDate.HasValue && !ps.StatisticsDate.HasValue).DefaultIfEmpty()
    select sp).Include(sp => sp.Environment)
                .Include(sp => sp.Platforms.Select(pl => pl.Participants.Select(pp => pp.Paymentsettlements.Select(ps => ps.Requester))))
                .Include(sp => sp.Platforms.Select(pl => pl.Participants.Select(pp => pp.Paymentsettlements.Select(ps => ps.Payer))))
                .ToList();

The result i'm looking for is that i always get the ServiceProvider no matter if there are objects inside the ServiceProvider. I am getting this result at the moment, but the where I've put in the query does not get taken into account. The following where does not make any difference:

innerPs.Where(ps => ps.ConfirmedOn.HasValue && ps.ExportDate.HasValue && !ps.StatisticsDate.HasValue).DefaultIfEmpty()

If the StatisticsDate has a value, those Paymentsettlements also are given in the output.

I've already tried to put the WHERE statement on the context.Paymentsettlements object.

I hope anyone can help me with this problem.

Kind regards,

Rob H

Actually you are doing left join and then selecting ServiceProviders. Here you are getting all providers. Then you are including all child elements: select sp).Include(sp => sp.Environment) . This won't work. It will include all rows.

What you can really do is select to anonymous type like

select new {sp, ps }

Unfortunately there is no way of filtering in included objects. Include is something like all or nothing. You can read about it:

How to filter nested collection Entity Framework objects?

EF Query With Conditional Include

I've finally made another (hacky) solution. Here is my final code:

using (var context = new BetaalplatformContext())
        {
            var dienstverleners = context.Dienstverleners.Include(dv => dv.Omgeving)
                                                         .Include(dv => dv.Platformen)
                                                         .Include(dv => dv.Platformen.Select(pl => pl.Deelnemers))
                                                         .Include(dv => dv.Platformen.Select(pl => pl.Deelnemers.Select(dn => dn.Betaalregelingen)))
                                                         .Include(dv => dv.Platformen.Select(pl => pl.Deelnemers.Select(dn => dn.Betaalregelingen.Select(br => br.Aanvrager))))
                                                         .Include(dv => dv.Platformen.Select(pl => pl.Deelnemers.Select(dn => dn.Betaalregelingen.Select(br => br.Betaler))))
                                                         .ToList();

            dienstverleners.ForEach(
                dv => dv.Platformen.ForEach(
                    pl => pl.Deelnemers.ForEach(
                        dn => dn.Betaalregelingen = dn.Betaalregelingen
                            .Where(br2 => br2.BevestigdOp.HasValue && br2.ExportDatum.HasValue && !br2.StatistiekDatum.HasValue)
                            .ToList()
                        )
                    )
                );

            return dienstverleners;
        }

This way i am abled to keep my models intact (I don't like to use anonymous objects).

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