Jerome
Jerome

Reputation: 42

LINQ determine if children of children exists

Currently I have a database design as such:

Movie 1..1-->0..* MovieRelease 1..1-->0..* Performance

I would like to write a LINQ statement to select all Movie that have one or more performance. This is what I came up with but it isn't working.

var instances = await GeneralModelRepository.GetQueryable<Movie>()
    .Include(x => x.MovieReleases)
    .ThenInclude(x => x.Performances)
    .Where(x => x.MovieReleases.Any())
    .Where(x => x.MovieReleases.All(x => x.Performances.Any()))
    .AsNoTracking().ToListAsync();

Upvotes: 0

Views: 62

Answers (1)

Svyatoslav Danyliv
Svyatoslav Danyliv

Reputation: 27282

Try the following query:

var instances = await GeneralModelRepository.GetQueryable<Movie>()
    .Include(x => x.MovieReleases)
        .ThenInclude(x => x.Performances)
    .Where(x => x.MovieReleases.SelectMany(r => r.Performances).Any())
    .AsNoTracking()
    .ToListAsync();

Upvotes: 1

Related Questions