C#C
C#4y ago
FusedQyou

❔ Unable to get automapper's projectTo() to work properly.

I need to use ProjectTo() to properly cast a query into a DTO, but in this case my user can only contains pictures which have Deleted = false. The code below works, but all photo's are returned, including deleted ones. The commented Include() works fine, but I need the mapping to happen on query level, and not after, because of the creation of a paginatedList. If I map later, the PaginatedList misses crucial information, and I do not want to change its behaviour.

public async Task<PaginatedList<DTODataUser>> GetAllDTOPaginatedAsync(PaginatedQuery pagination, bool includeDeleted, CancellationToken cancellationToken)
    {
        var userQuery = this._databaseContext
            .Users
            .Where(x => !includeDeleted ? !x.Deleted : true)
            .ProjectTo<DTODataUser>(this._mapper.ConfigurationProvider, null, (user) => user.Photos.Where(y => !includeDeleted ? !y.Deleted : true));
        //.Include(x => x.Photos.Where(y => !includeDeleted ? !y.Deleted : true))

        var users = await PaginatedList<DTODataUser>.CreateAsync(userQuery, pagination, cancellationToken);
        return users;
    }


How can I replicate
Include
to work with projectTo? As said above, the current version is allowed, but deleted photo's are returned too.
Was this page helpful?