© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
27 replies
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
Deleted = false
. The code below works, but all photo's are returned, including deleted ones. The commented
Include()
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;
    }
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
Include
to work with projectTo? As said above, the current version is allowed, but deleted photo's are returned too.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

IValueResolver won't work with AutoMapper
C#CC# / help
2y ago
Unable to get localization to get to work in MVC .net8
C#CC# / help
2y ago
Unable to get the FieldInfo's value
C#CC# / help
2y ago
❔ ✅ AutoMapper DI
C#CC# / help
3y ago