help
Root Question Message
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;
}
Include
to work with projectTo? As said above, the current version is allowed, but deleted photo's are returned too..ExplicitExpansion()
, but this did not change its behaviour.Where()
instead of that .ExplicitExpansion()
..?includeDeleted
boolean is set.Select()
Expression<Func<TSource, TTarget>>
properties and use thosestatic class UserMappings
{
public static Expression<Func<User, UserDto>> ToDto = user => new UserDto {
Name = user.UserName,
Age = user.Age,
Comments = user.Comments.Where(c => !c.IsDeleted)
}
public static Expression<Func<User, UserDto>> ToDtoWithParam(int param) => user => new UserDto {
Name = user.UserName,
Age = user.Age,
Whatever = param > 10 ? user.Foo : user.Bae
}
}
Expression<Func<,>>
.Select(UserMappings.ToDto)
IQueryable
public static IQueryable<T> Paginate<T>(this IQueryable<T> query, int page, int perPage)
{
if (page < 1)
throw new ArgumentOutOfRangeException(nameof(page), "Page has to be greater than 0");
if (perPage < 1)
throw new ArgumentOutOfRangeException(nameof(perPage), "PerPage has to be greater than 0");
return query
.Skip(Math.Max(0, page - 1) * perPage)
.Take(perPage);
}