LINQ expression could not be translated querying child list properties

48 Views Asked by At

Doing something that seems really simple using EF, DbContext and CosmosDB. Trying to get a list of items based on a field in a child list being equal to a parameter.

Source of the predicate to the repository:

public async Task<IReadOnlyCollection<GameResponse>> Handle(GetMyUpcomingGamesQuery request, CancellationToken cancellationToken)
{
    var test = await _repository.GetAsync(p => p.Players.Any(z=>z.Id == request.ProfileId));

...

Source of the repository:

public async Task<IReadOnlyCollection<GameReadModel>> GetAsync(Expression<Func<GameReadModel, bool>> predicate)
{
    return await _context.GameReadModels.AsNoTracking().Where(predicate).ToListAsync();
}

Source of read model:

namespace Common.Domain.ReadModels
{
    public class GameReadModel 
    {
        public string Id { get; set; }

        public VenueReadModel Venue { get; set; } 

        public string Notes { get; set; }

        public DateTimeOffset Begins { get; set; }

        public DateTimeOffset Ends { get; set; }

        public int NumberOfPlayers { get; set; }

        public decimal MinimumRating { get; set; }

        public decimal MaximumRating { get; set; }

        public bool OpenToLobby { get; set; }

        public string GameType { get; set; }

        public ProfileReadModel Host { get; set; } 

        public IReadOnlyCollection<ProfileReadModel> Players { get; set; } = new List<ProfileReadModel>();  
        
        public IReadOnlyCollection<ListItemReadModel> Lists { get; set; } = new List<ListItemReadModel>();

        public DateTimeOffset Created { get; set; }

        public DateTimeOffset Modified { get; set; }
    }
}

Error message:

System.InvalidOperationException: The LINQ expression 'DbSet() .Where(g => EF.Property<IReadOnlyCollection>(g, "Players") .AsQueryable() .Any(o => o.Id == __request_ProfileId_0))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.

If I first query the list using a property value OTHER THAN the child list, it works fine. To do this in more than one step however would result in a LOT of extraneous fetching.

It feels like the child list is not materialized or available for the subquery in some way.

Any help appreciated.

0

There are 0 best solutions below